Advertisement
Guest User

Digital LightWand + SD + LCD

a guest
Nov 26th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.30 KB | None | 0 0
  1. #include <SD.h>
  2. #include <LEDStrip.h>
  3. #include <LiquidCrystal.h>
  4.  
  5.  
  6. #define SIPIN 30
  7. #define DIPIN 31
  8. #define CLKPIN 32
  9. #define LATCHPIN 33
  10. LEDStrip mystrip(DIPIN,SIPIN,LATCHPIN,CLKPIN);
  11. #define SPULSES 5000
  12. #define STRIP_LENGTH 48
  13. #define BACKLIGHT 10
  14. byte BL = B10000000; // Black / Off
  15. #define SDssPin 53  //SD card CS pin
  16. int frameDelay = 10; // default for the frame delay
  17. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  //Init the LCD
  18.  
  19. //BacklightControl to save battery Life
  20. boolean BackLightTimer = false;
  21. int BackLightTimeout = 1000;
  22. int BackLightTemp =  BackLightTimeout;
  23.  
  24. //Stuff for the Keypad
  25. int adc_key_val[5] ={
  26.   30, 150, 360, 535, 760 };
  27. int NUM_KEYS = 5;
  28. int adc_key_in;
  29. int key=-1;
  30. int oldkey=-1;
  31.  
  32. File root;
  33. File dataFile;
  34. String m_CurrentFilename = "";
  35. int m_FileIndex = 0;
  36. int m_NumberOfFiles = 0;
  37. String m_FileNames[200]; //yep this is bad, but unless you are going to have over 200 images on your lightwand..
  38.  
  39. long buffer[STRIP_LENGTH];
  40. char tmpbuffer[20];
  41. long lCntr[1];
  42.  
  43. struct bitmapblock {
  44.   byte len;
  45.   byte r;
  46.   byte g;
  47.   byte b;
  48. }
  49. mybitmapblock;
  50.  
  51. char buf[4];
  52. int fileVersion;
  53. uint32_t bitmapOffset;
  54. int bitmapHeight;
  55. uint32_t bitmapWidth;
  56. int StartDelay;
  57. int EndDelay;
  58. int StrobeOn;
  59. int StrobeOff;
  60. byte VReversed;
  61. byte HReversed;
  62.  
  63. //Colour weights for the HL1606..
  64. uint8_t HL1606highred = 100;
  65. uint8_t HL1606highgreen = 100;
  66. uint8_t HL1606highblue = 100;
  67. uint8_t HL1606lowred = 20;
  68. uint8_t HL1606lowgreen = 20;
  69. uint8_t HL1606lowblue = 20;
  70.  
  71.  
  72.  
  73. void setup()
  74. {
  75.   Serial.begin(9600);
  76.   setupLEDs();
  77.   setupLCDdisplay();
  78.   setupSDcard();
  79. }
  80.  
  81. void setupLEDs()
  82. {
  83.   ClearStrip(100);
  84. }
  85.  
  86. void setupLCDdisplay()
  87. {
  88.   lcd.begin(16, 2);
  89.   lcd.print("  *** DLW ***");
  90.   lcd.setCursor(0, 1);
  91.   lcd.print("Initializing...");
  92.   delay(1000);  
  93.   lcd.clear();
  94.   pinMode(BACKLIGHT, OUTPUT);
  95.   digitalWrite(BACKLIGHT,LOW);
  96.  
  97. }
  98.  
  99. void setupSDcard()
  100. {
  101.   pinMode(SDssPin, OUTPUT);
  102.  
  103.   while (!SD.begin(SDssPin)) {
  104.     lcd.print("SD init failed!");
  105.     delay(1000);
  106.     lcd.clear();
  107.     delay(500);
  108.   }
  109.   lcd.print("SD init done.");
  110.   delay(1000);
  111.   root = SD.open("/");
  112.   lcd.clear();
  113.   lcd.print("Scanning files");
  114.   GetFileNamesFromSD(root);
  115.   isort(m_FileNames, m_NumberOfFiles);
  116.   m_CurrentFilename = m_FileNames[0];
  117.   DisplayCurrentFilename();
  118. }
  119.  
  120. void latchanddelay(int dur)
  121. {
  122.   mystrip.latch();
  123.   delay(dur);
  124. }
  125.  
  126. void ClearStrip(int duration)
  127. {
  128.   int x;
  129.   for(x=0;x<STRIP_LENGTH;x++)
  130.   {
  131.     mystrip.pushCmd(BL);
  132.   }
  133.   latchanddelay(duration);
  134. }
  135.  
  136. void MultiPush(int Cmd, int Cnt) {
  137.   while(Cnt-->0) mystrip.pushCmd(Cmd);
  138. }
  139.  
  140. int ReadKeypad()
  141. {
  142.   adc_key_in = analogRead(0);    // read the value from the sensor  
  143.   digitalWrite(13, HIGH);  
  144.   key = get_key(adc_key_in);                // convert into key press
  145.  
  146.   if (key != oldkey)                    // if keypress is detected
  147.   {
  148.     delay(50);      // wait for debounce time
  149.     adc_key_in = analogRead(0);    // read the value from the sensor  
  150.     key = get_key(adc_key_in);              // convert into key press
  151.     if (key != oldkey)             
  152.     {          
  153.       oldkey = key;
  154.       if (key >=0){
  155.         return key;
  156.       }
  157.     }
  158.   }
  159.   return key;
  160. }
  161.  
  162.  
  163. // Convert ADC value to key number
  164. int get_key(unsigned int input)
  165. {
  166.   int k;
  167.   for (k = 0; k < NUM_KEYS; k++)
  168.   {
  169.     if (input < adc_key_val[k])
  170.     {        
  171.       return k;
  172.     }
  173.   }
  174.   if (k >= NUM_KEYS)
  175.     k = -1;     // No valid key pressed
  176.   return k;
  177. }
  178.  
  179.  
  180. //The Main menu starts here...
  181. void loop()
  182. {
  183.   int keypress = ReadKeypad();
  184.   if ( keypress == 1) //up key (step up through the filenames)
  185.   {
  186.     BackLightOn();
  187.     if (m_FileIndex > 0)
  188.     {
  189.       m_FileIndex--;
  190.     }
  191.     else
  192.     {
  193.       m_FileIndex = m_NumberOfFiles -1; //wrap round to the last file
  194.     }
  195.  
  196.     DisplayCurrentFilename();
  197.     delay(500);
  198.   }
  199.  
  200.   if ( keypress == 2) //down key (step down through the filenames)
  201.   {
  202.     BackLightOn();
  203.     if (m_FileIndex < m_NumberOfFiles -1)
  204.     {
  205.       m_FileIndex++;
  206.     }
  207.     else
  208.     {
  209.       m_FileIndex = 0;//wrap round to the 1st file again
  210.     }
  211.     DisplayCurrentFilename();
  212.     delay(500);
  213.   }
  214.  
  215.   if (keypress == 4)//select key (send out the selected file)
  216.   {
  217.     SendFile(m_CurrentFilename);
  218.   }
  219.  
  220.   if(keypress == 0) //right key (frame delay +)
  221.   {
  222.     BackLightOn();
  223.     if (frameDelay < 200)
  224.     {
  225.       frameDelay+=5;
  226.     }
  227.     ShowFrameDelay();
  228.   }
  229.  
  230.   if(keypress == 3)//left key (frame delay -)
  231.   {
  232.     BackLightOn();
  233.     if (frameDelay > 5)
  234.     {
  235.       frameDelay-=5;
  236.     }
  237.     ShowFrameDelay();
  238.   }
  239.  
  240.   if (BackLightTimer == true) BackLightTime();
  241.  
  242. }
  243.  
  244. void BackLightOn()
  245. {
  246.   digitalWrite(BACKLIGHT,HIGH);
  247.   BackLightTimer = true;
  248.   int BackLightTemp =  BackLightTimeout;
  249. }
  250.  
  251. void BackLightTime()
  252. {
  253.   if (BackLightTemp <= 0)
  254.   {
  255.     BackLightTimer = false;
  256.     BackLightTemp =  BackLightTimeout;
  257.     digitalWrite(BACKLIGHT,LOW);
  258.   }
  259.   else
  260.   {
  261.     BackLightTemp --;
  262.     delay(1);
  263.   }
  264. }
  265.  
  266. void ShowFrameDelay()
  267. {
  268.   lcd.clear();
  269.   lcd.print("Frame delay:");
  270.   lcd.setCursor(0,1);
  271.   lcd.print(frameDelay);
  272.   delay(500);
  273.   DisplayCurrentFilename();
  274. }
  275.  
  276.  
  277. void SendFile(String Filename)
  278. {
  279.   lcd.clear();
  280.   lcd.print("Sending File");
  281.   lcd.setCursor(0, 1);
  282.   lcd.print(Filename);
  283.   char temp[14];
  284.   Filename.toCharArray(temp,14);
  285.  
  286.   dataFile = SD.open(temp);
  287.  
  288.   // if the file is available send it to the LED's
  289.   if (dataFile)
  290.   {
  291.     ReadTheFile();
  292.     dataFile.close();
  293.     ClearStrip(100);
  294.   }  
  295.   else
  296.   {
  297.     lcd.clear();
  298.     lcd.print("Error reading file");
  299.     delay(1000);
  300.  
  301.     setupSDcard();//try and re-init the SD card...
  302.     return;
  303.   }
  304.   DisplayCurrentFilename();
  305. }
  306.  
  307. void DisplayCurrentFilename()
  308. {
  309.   m_CurrentFilename = m_FileNames[m_FileIndex];
  310.   lcd.clear();
  311.   lcd.print(m_CurrentFilename);
  312. }
  313.  
  314. void GetFileNamesFromSD(File dir)
  315. {
  316.   int fileCount = 0;
  317.   String CurrentFilename = "";
  318.   while(1)
  319.   {
  320.     File entry =  dir.openNextFile();
  321.     if (! entry) {
  322.       // no more files
  323.       m_NumberOfFiles = fileCount;
  324.       break;
  325.     }
  326.     else
  327.     {
  328.       if (entry.isDirectory()) {
  329.         //GetNextFileName(root);
  330.       }
  331.       else {
  332.         CurrentFilename = entry.name();
  333.         if (CurrentFilename.endsWith(".LSB"))//find files with our extension only
  334.         {
  335.           m_FileNames[fileCount] = entry.name();
  336.           fileCount++;
  337.         }
  338.       }
  339.     }
  340.   }
  341. }
  342.  
  343. uint32_t readLong()
  344. {
  345.   uint32_t retValue;
  346.   int incomingbyte;
  347.  
  348.   incomingbyte=readByte();
  349.   retValue=(uint32_t)((byte)incomingbyte)<<24;
  350.  
  351.   incomingbyte=readByte();
  352.   retValue+=(uint32_t)((byte)incomingbyte)<<16;
  353.  
  354.   incomingbyte=readByte();
  355.   retValue+=(uint32_t)((byte)incomingbyte)<<8;
  356.  
  357.   incomingbyte=readByte();
  358.   retValue+=(uint32_t)((byte)incomingbyte);
  359.  
  360.   return retValue;
  361. }
  362.  
  363. int readInt()
  364. {
  365.   int incomingbyte;
  366.   uint16_t retValue;
  367.  
  368.   incomingbyte=readByte();
  369.   retValue+=(int)((byte)incomingbyte)<<8;
  370.  
  371.   incomingbyte=readByte();
  372.   retValue+=(int)((byte)incomingbyte);
  373.  
  374.   return retValue;
  375. }
  376.  
  377. int readByte()
  378. {
  379.   int retbyte=-1;
  380.   while(retbyte<0) retbyte= dataFile.read();
  381.   return retbyte;
  382. }
  383.  
  384. void writeRGB(uint8_t Red,uint8_t Green,uint8_t Blue)
  385. {
  386.   byte colorByte = 128;
  387.   //
  388.   //My strip seemed to have B + G swapped so I altered it here!
  389.   //
  390.   if (Blue > HL1606highblue) colorByte |= 16; //Bright Blue
  391.   else if (Blue > HL1606lowblue) colorByte |= 32; //Dim Blue
  392.   if (Red > HL1606highred) colorByte |= 4; //Bright Red
  393.   else if (Red > HL1606lowred) colorByte |= 8; //Dim Red
  394.   if (Green > HL1606highgreen) colorByte |= 1; //Bright Green
  395.   else if (Green > HL1606lowgreen) colorByte |= 2; //Dim Green
  396.   mystrip.pushCmd(colorByte);
  397. }
  398.  
  399.  
  400. void post_frame (void) {
  401.   for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++)
  402.   {
  403.     long this_led_color = buffer[LED_number]; //24 bits of color data
  404.     int r=(((this_led_color>>16) & 0xff)>>1);
  405.     int g=(((this_led_color>>8) & 0xff)>>1);
  406.     int b=(((this_led_color) & 0xff)>>1);
  407.     writeRGB(r,g,b);
  408.   }
  409.   latchanddelay(frameDelay);
  410. }
  411.  
  412. void ReadTheFile()
  413. {
  414.   long WAND=readLong();
  415.   fileVersion=readByte();
  416.   bitmapOffset = readLong();
  417.   bitmapHeight = readInt();
  418.   bitmapWidth = readLong();
  419.   StartDelay = readByte();
  420.   EndDelay = readByte();
  421.   StrobeOn = readInt();
  422.   StrobeOff = readInt();
  423.   VReversed = readByte();
  424.   HReversed = readByte();
  425.  
  426.   Serial.println("file version ");
  427.   Serial.println(fileVersion,DEC);
  428.   Serial.println("bitmap offset");
  429.   Serial.println(bitmapOffset,DEC);
  430.   Serial.println("bitmap height");
  431.   Serial.println(bitmapHeight,DEC);
  432.   Serial.println("bitmap Width");
  433.   Serial.println(bitmapWidth,DEC);
  434.   Serial.println("start delay");
  435.   Serial.println(StartDelay,DEC);
  436.   Serial.println("end delay");
  437.   Serial.println(EndDelay,DEC);
  438.   Serial.println("strobe off");
  439.   Serial.println(StrobeOff,DEC);
  440.   Serial.println("strobe on");
  441.   Serial.println(StrobeOn,DEC);
  442.   Serial.println("vReversed");
  443.   Serial.println(VReversed,DEC);
  444.   Serial.println("hReversed");
  445.   Serial.println(HReversed,DEC);
  446.  
  447.   if(readLong()!=0L) Serial.println("Invalid file");
  448.   else Serial.println("Read in the header successfully");
  449.  
  450.   //ClearStrip(100);
  451.   long filepos=0;
  452.  
  453.   for(int x=0;x<bitmapWidth;x++) {
  454.     int bufpos=0;
  455.     //Serial.println("Serial buffer is at ");
  456.     //Serial.println(Serial1.available(),DEC);
  457.     //  Serial.println("At column" + x );
  458.     while(bufpos<bitmapHeight) {
  459.       mybitmapblock.len=readByte();
  460.       mybitmapblock.r=readByte();
  461.       mybitmapblock.g=readByte();
  462.       mybitmapblock.b=readByte();
  463.  
  464.       filepos=filepos+4;
  465.       for(int j=0;j<mybitmapblock.len;j++) {
  466.         buffer[bufpos]=(long)mybitmapblock.r << 16;
  467.         buffer[bufpos]|=(long)mybitmapblock.g << 8;
  468.         buffer[bufpos++]|=(long)mybitmapblock.b;
  469.       }
  470.       if(bufpos>bitmapHeight)
  471.       {
  472.         Serial.println("FATAL ERROR - Problem with length of bitmap");
  473.         while(1) delay(1);
  474.       }
  475.     }
  476.     post_frame();
  477.   }
  478.   Serial.println("Finished");
  479.   ClearStrip(100);
  480. }
  481.  
  482. //sort the filenames in alphabetical order
  483. void isort(String *filenames, int n)
  484. {
  485.   for (int i = 1; i < n; ++i)
  486.   {
  487.     String j = filenames[i];
  488.     int k;
  489.     for (k = i - 1; (k >= 0) && (j < filenames[k]); k--)
  490.     {
  491.       filenames[k + 1] = filenames[k];
  492.     }
  493.     filenames[k + 1] = j;
  494.   }
  495. }
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement