Advertisement
Guest User

Digital Light Wand + LCD + SD + WS2801+ BMP direct

a guest
May 18th, 2013
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.82 KB | None | 0 0
  1. //
  2. // Arduino Digital Light Wand + SD + LCD V1.07 (ws2801)
  3. // by Is0-Mick 2012
  4. //
  5. // This version is for ws2801 strips (led pixels)
  6. //
  7. // Added support for external control switches, for UP and Down to select the filename.
  8. //
  9. //
  10. // This version can use dual strips for double the resolution (change dualstrip = false to true)
  11. //
  12. // This version supports direct reading of a 24bit BMP from the SD card.
  13. // You need to rotate the image 90 degrees right, due to lack of memory / speed to process
  14. // this on the fly...
  15. //
  16. // Added Gamma Table from ada fruit code which gives better conversion of 24 bit to 21 bit
  17. // instead of just bitshifting values.
  18. // Also added version number to display on start up.
  19. //
  20. // Fixed a strange problem with 1 green pixel being left on if the last line of the image
  21. // or last pixel was not black. Apparently seems to be a known hardware / library bug...
  22. //
  23. // Tidied up code
  24. //
  25. // Fixed Backlight Problem
  26. //
  27. // Fixed the SD init problem (removing card, trying to send a file (which fails) now re-inits the card.
  28. // Backlight now fades out, instead of just switching off.
  29. //
  30. // Changed one of the original values of the shields key values, as it was thinking you were pressing a different
  31. // button sometimes, so changed the 535 value to 600 which seems to fix that :)
  32. //
  33. // Added code for an external switch to show the bitmap so you don't have fiddle with the small button on the shield
  34. // Pins are defined as aux button. I pulled one low as a GND, and the other is the trigger.(pins 44,45 currently)
  35.  
  36.  
  37.  
  38.  
  39. #include <SPI.h>
  40. #include <Adafruit_WS2801.h>
  41. #include <SD.h>
  42. #include <LiquidCrystal.h>
  43. #include <SPI.h>
  44.  
  45.  
  46. #define BACKLIGHT 10
  47. #define SDssPin 53  //SD card CS pin
  48. int frameDelay = 10; // default for the frame delay
  49. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  //Init the LCD
  50.  
  51. int dataPin = 30;
  52. int clockPin = 31;
  53. byte gamma(byte x);
  54.  
  55.  
  56. #define STRIP_LENGTH 104
  57. uint16_t length = STRIP_LENGTH;
  58. boolean dualstrip = false;//set to true for dual strips
  59.  
  60. Adafruit_WS2801 strip = Adafruit_WS2801(length, dataPin, clockPin);
  61.  
  62. //BacklightControl to save battery Life
  63. boolean BackLightTimer = false;
  64. int BackLightTimeout = 2000;
  65. int BackLightTemp =  BackLightTimeout;
  66.  
  67. //Stuff for the Keypad
  68. //int adc_key_val[5] ={
  69. //  30, 150, 360, 535, 760 };
  70.  
  71. int adc_key_val[5] ={
  72.   30, 150, 360, 600, 760 };  
  73. int NUM_KEYS = 5;
  74. int adc_key_in;
  75. int key=-1;
  76. int oldkey=-1;
  77.  
  78. //AuxButton is a seperate button to send the image. Connect wires to these pins to use it.
  79. int AuxButton = 44;
  80. int AuxButtonGND = 45;
  81.  
  82. //Up Button is a seperate button to move up the filenames. Connect wires to these pins to use it.
  83. int UpButton =42;
  84. int UpButtonGND = 43;
  85.  
  86. //Down Button is a seperate button to move down the filenames. Connect wires to these pins to use it.
  87. int DownButton =40;
  88. int DownButtonGND =41;
  89.  
  90. File root;
  91. File dataFile;
  92. String m_CurrentFilename = "";
  93. int m_FileIndex = 0;
  94. int m_NumberOfFiles = 0;
  95. String m_FileNames[200]; //yep this is bad, but unless you are going to have over 200 images on your lightwand..
  96.  
  97. long buffer[STRIP_LENGTH];
  98.  
  99. void setup()
  100. {
  101.   Serial.begin(115200);
  102.   pinMode(AuxButton, INPUT);
  103.   digitalWrite(AuxButton,HIGH);
  104.   pinMode(AuxButtonGND, OUTPUT);
  105.   digitalWrite(AuxButtonGND,LOW);
  106.  
  107.   pinMode(UpButton, INPUT);
  108.   digitalWrite(UpButton,HIGH);
  109.   pinMode(UpButtonGND, OUTPUT);
  110.   digitalWrite(UpButtonGND,LOW);
  111.  
  112.   pinMode(DownButton, INPUT);
  113.   digitalWrite(DownButton,HIGH);
  114.   pinMode(DownButtonGND, OUTPUT);
  115.   digitalWrite(DownButtonGND,LOW);
  116.   setupLEDs();
  117.   setupLCDdisplay();
  118.   setupSDcard();
  119.   BackLightOn();
  120. }
  121.  
  122. void setupLEDs()
  123. {
  124.   strip.begin();
  125.   strip.show();
  126. }
  127.  
  128. void setupLCDdisplay()
  129. {
  130.   lcd.begin(16,2);
  131.   lcd.print("*** DLW V.07 ***");
  132.   lcd.setCursor(0, 1);
  133.   lcd.print("Initializing...");
  134.   delay(1000);  
  135.   lcd.clear();
  136. }
  137.  
  138. void setupSDcard()
  139. {
  140.   pinMode(SDssPin, OUTPUT);
  141.  
  142.   while (!SD.begin(SDssPin)) {
  143.     BackLightOn();
  144.     lcd.print("SD init failed!");
  145.     delay(1000);
  146.     lcd.clear();
  147.     delay(500);
  148.   }
  149.   lcd.clear();
  150.   lcd.print("SD init done.");
  151.   delay(1000);
  152.   root = SD.open("/");
  153.   lcd.clear();
  154.   lcd.print("Scanning files");
  155.   delay(500);
  156.   GetFileNamesFromSD(root);
  157.   isort(m_FileNames, m_NumberOfFiles);
  158.   m_CurrentFilename = m_FileNames[0];
  159.   DisplayCurrentFilename();
  160.  
  161. }
  162.  
  163. int ReadKeypad()
  164. {
  165.   adc_key_in = analogRead(0);    // read the value from the sensor  
  166.   digitalWrite(13, HIGH);  
  167.   key = get_key(adc_key_in);                // convert into key press
  168.   //Serial.print("key read = ");
  169.   //Serial.println(adc_key_in,DEC);
  170.  
  171.   if (key != oldkey)                    // if keypress is detected
  172.   {
  173.     delay(50);      // wait for debounce time
  174.     adc_key_in = analogRead(0);    // read the value from the sensor  
  175.     key = get_key(adc_key_in);              // convert into key press
  176.     if (key != oldkey)             
  177.     {          
  178.       oldkey = key;
  179.       if (key >=0){
  180.         return key;
  181.       }
  182.     }
  183.   }
  184.   return key;
  185. }
  186.  
  187.  
  188. // Convert ADC value to key number
  189. int get_key(unsigned int input)
  190. {
  191.   int k;
  192.   for (k = 0; k < NUM_KEYS; k++)
  193.   {
  194.     if (input < adc_key_val[k])
  195.     {        
  196.       return k;
  197.     }
  198.   }
  199.   if (k >= NUM_KEYS)
  200.     k = -1;     // No valid key pressed
  201.   return k;
  202. }
  203.  
  204.  
  205. //The Main menu starts here...
  206. void loop()
  207. {
  208.   int keypress = ReadKeypad();
  209.   if (( keypress == 1) || (digitalRead(UpButton) == LOW)) //up key (step up through the filenames)
  210.   {
  211.     BackLightOn();
  212.     if (m_FileIndex > 0)
  213.     {
  214.       m_FileIndex--;
  215.     }
  216.     else
  217.     {
  218.       m_FileIndex = m_NumberOfFiles -1; //wrap round to the last file
  219.     }
  220.  
  221.     DisplayCurrentFilename();
  222.     delay(500);
  223.   }
  224.  
  225.   if (( keypress == 2) || (digitalRead(DownButton) == LOW)) //down key (step down through the filenames)
  226.   {
  227.     BackLightOn();
  228.     if (m_FileIndex < m_NumberOfFiles -1)
  229.     {
  230.       m_FileIndex++;
  231.     }
  232.     else
  233.     {
  234.       m_FileIndex = 0;//wrap round to the 1st file again
  235.     }
  236.     DisplayCurrentFilename();
  237.     delay(500);
  238.   }
  239.   //Serial.print(digitalRead(AuxButton),DEC);//for displaying the key values
  240.  
  241.   if ((keypress == 4) || (digitalRead(AuxButton) == LOW))//select key (send out the selected file)
  242.   {
  243.     SendFile(m_CurrentFilename);
  244.   }
  245.  
  246.   if(keypress == 0) //right key (frame delay +)
  247.   {
  248.     BackLightOn();
  249.     if (frameDelay < 200)
  250.     {
  251.       frameDelay+=5;
  252.     }
  253.     ShowFrameDelay();
  254.   }
  255.  
  256.   if(keypress == 3)//left key (frame delay -)
  257.   {
  258.     BackLightOn();
  259.     if (frameDelay > 5)
  260.     {
  261.       frameDelay-=5;
  262.     }
  263.     ShowFrameDelay();
  264.   }
  265.  
  266.   if (BackLightTimer == true) BackLightTime();
  267.  
  268. }
  269.  
  270. void BackLightOn()
  271. {
  272.   analogWrite(BACKLIGHT,255);
  273.   BackLightTimer = true;
  274.   BackLightTemp =  BackLightTimeout;
  275. }
  276.  
  277. void BackLightTime()
  278. {
  279.   if ((BackLightTemp <= 255) && (BackLightTemp >= 0))
  280.   {
  281.     analogWrite(BACKLIGHT,BackLightTemp);
  282.     delay(1);
  283.   }
  284.  
  285.   if (BackLightTemp <= 0)
  286.   {
  287.     BackLightTimer = false;
  288.     BackLightTemp =  BackLightTimeout;
  289.     analogWrite(BACKLIGHT,0);
  290.   }
  291.   else
  292.   {
  293.     BackLightTemp --;
  294.     delay(1);
  295.   }
  296. }
  297.  
  298. void ShowFrameDelay()
  299. {
  300.   lcd.clear();
  301.   lcd.print("Frame delay:");
  302.   lcd.setCursor(0,1);
  303.   lcd.print(frameDelay);
  304.   delay(500);
  305.   DisplayCurrentFilename();
  306. }
  307.  
  308.  
  309. void SendFile(String Filename)
  310. {
  311.   lcd.clear();
  312.   lcd.print("Sending File");
  313.   lcd.setCursor(0, 1);
  314.   lcd.print(Filename);
  315.   char temp[14];
  316.   Filename.toCharArray(temp,14);
  317.  
  318.   dataFile = SD.open(temp);
  319.  
  320.   // if the file is available send it to the LED's
  321.   if (dataFile)
  322.   {
  323.     ReadTheFile();
  324.     dataFile.close();
  325.     ClearStrip(100);
  326.   }  
  327.   else
  328.   {
  329.     lcd.clear();
  330.     lcd.print("  Error reading");
  331.     lcd.setCursor(4, 1);
  332.     lcd.print("file");
  333.     BackLightOn();
  334.     delay(1000);
  335.     lcd.clear();
  336.     setupSDcard();//try to re-init the SD card...(this was failing, but a fix can be done below)
  337.     //In the SD.CPP in the BEGIN class which starts
  338.     // boolean SDClass::begin(uint8_t csPin) {
  339.     //
  340.     //it needs the line below to be added
  341.     //
  342.     // if (root.isOpen()) root.close(); // allows repeated calls
  343.     //
  344.     // Just before the line
  345.     // return card.init(SPI_HALF_SPEED, csPin) &&
  346.     //
  347.     //
  348.  
  349.  
  350.     return;
  351.   }
  352.   DisplayCurrentFilename();
  353. }
  354.  
  355. void DisplayCurrentFilename()
  356. {
  357.   m_CurrentFilename = m_FileNames[m_FileIndex];
  358.   lcd.clear();
  359.   lcd.print(m_CurrentFilename);
  360. }
  361.  
  362. void GetFileNamesFromSD(File dir)
  363. {
  364.   int fileCount = 0;
  365.   String CurrentFilename = "";
  366.   while(1)
  367.   {
  368.     File entry =  dir.openNextFile();
  369.     if (! entry) {
  370.       // no more files
  371.       m_NumberOfFiles = fileCount;
  372.       break;
  373.     }
  374.     else
  375.     {
  376.       if (entry.isDirectory()) {
  377.         //GetNextFileName(root);
  378.       }
  379.       else {
  380.         CurrentFilename = entry.name();
  381.         if (CurrentFilename.endsWith(".bmp") || CurrentFilename.endsWith(".BMP") )//find files with our extension only
  382.         {
  383.           m_FileNames[fileCount] = entry.name();
  384.           fileCount++;
  385.         }
  386.       }
  387.     }
  388.   }
  389. }
  390.  
  391.  
  392. void latchanddelay(int dur)
  393. {
  394.   strip.show();
  395.   delay(dur);
  396. }
  397.  
  398. void ClearStrip(int duration)
  399. {
  400.   int x;
  401.   for(x=0;x<STRIP_LENGTH;x++)
  402.   {
  403.     strip.setPixelColor(x, 0);
  404.   }
  405.   strip.show();// Had to add this extra show, otherwise if you were displaying an image
  406.   // and the last line / pixel were white, a green pixel would remain lit!
  407.   latchanddelay(duration);
  408.  
  409. }
  410.  
  411. uint32_t readLong()
  412. {
  413.   uint32_t retValue;
  414.   byte incomingbyte;
  415.  
  416.   incomingbyte=readByte();
  417.   retValue=(uint32_t)((byte)incomingbyte);
  418.  
  419.   incomingbyte=readByte();
  420.   retValue+=(uint32_t)((byte)incomingbyte)<<8;
  421.  
  422.   incomingbyte=readByte();
  423.   retValue+=(uint32_t)((byte)incomingbyte)<<16;
  424.  
  425.   incomingbyte=readByte();
  426.   retValue+=(uint32_t)((byte)incomingbyte)<<24;
  427.  
  428.   return retValue;
  429. }
  430.  
  431. uint16_t readInt()
  432. {
  433.   byte incomingbyte;
  434.   uint16_t retValue;
  435.  
  436.   incomingbyte=readByte();
  437.   retValue+=(uint16_t)((byte)incomingbyte);
  438.  
  439.   incomingbyte=readByte();
  440.   retValue+=(uint16_t)((byte)incomingbyte)<<8;
  441.  
  442.   return retValue;
  443. }
  444.  
  445. int readByte()
  446. {
  447.   int retbyte=-1;
  448.   while(retbyte<0) retbyte= dataFile.read();
  449.   return retbyte;
  450. }
  451.  
  452.  
  453. void ReadTheFile()
  454. {
  455. #define MYBMP_BF_TYPE           0x4D42
  456. #define MYBMP_BF_OFF_BITS       54
  457. #define MYBMP_BI_SIZE           40
  458. #define MYBMP_BI_RGB            0L
  459. #define MYBMP_BI_RLE8           1L
  460. #define MYBMP_BI_RLE4           2L
  461. #define MYBMP_BI_BITFIELDS      3L
  462.  
  463.  
  464.  
  465.   uint16_t bmpType = readInt();
  466.   uint32_t bmpSize = readLong();
  467.   uint16_t bmpReserved1 = readInt();
  468.   uint16_t bmpReserved2 = readInt();
  469.   uint32_t bmpOffBits = readLong();
  470.   bmpOffBits = 54;
  471.  
  472.   Serial.println("bmpType = ");
  473.   Serial.println(bmpType,HEX);
  474.  
  475.   Serial.println("bmpSize");
  476.   Serial.println(bmpSize,DEC);
  477.  
  478.   Serial.println("bmpReserved1");
  479.   Serial.println(bmpReserved1,DEC);
  480.  
  481.   Serial.println("bmpReserved2");
  482.   Serial.println(bmpReserved2,DEC);
  483.  
  484.  
  485.   Serial.println("bmpOffBits");
  486.   Serial.println(bmpOffBits,DEC);
  487.  
  488.   /* Check file header */
  489.   if (bmpType != MYBMP_BF_TYPE || bmpOffBits != MYBMP_BF_OFF_BITS)
  490.   {
  491.     lcd.setCursor(0, 0);
  492.     lcd.print("not a bitmap");
  493.     delay(1000);
  494.     return;
  495.   }
  496.  
  497.   /* Read info header */
  498.   uint32_t imgSize = readLong();
  499.   uint32_t imgWidth = readLong();
  500.   uint32_t imgHeight = readLong();
  501.   uint16_t imgPlanes = readInt();
  502.   uint16_t imgBitCount = readInt();
  503.   uint32_t imgCompression = readLong();
  504.   uint32_t imgSizeImage = readLong();
  505.   uint32_t imgXPelsPerMeter = readLong();
  506.   uint32_t imgYPelsPerMeter = readLong();
  507.   uint32_t imgClrUsed = readLong();
  508.   uint32_t imgClrImportant = readLong();
  509.  
  510.  
  511.   Serial.println("bitmap height");
  512.   Serial.println(imgHeight,DEC);
  513.   Serial.println("bitmap Width");
  514.   Serial.println(imgWidth,DEC);
  515.   Serial.println("bitmap bpp");
  516.   Serial.println(imgBitCount,DEC);
  517.  
  518.   /* Check info header */
  519.   if( imgSize != MYBMP_BI_SIZE || imgWidth <= 0 ||
  520.     imgHeight <= 0 || imgPlanes != 1 ||
  521.     imgBitCount != 24 || imgCompression != MYBMP_BI_RGB ||
  522.     imgSizeImage == 0 )
  523.   {
  524.     lcd.setCursor(0, 0);
  525.     lcd.print("Unsupported");
  526.     lcd.setCursor(0, 1);
  527.     lcd.print("Bitmap Use 24bpp");
  528.     delay(1000);
  529.     return;
  530.   }
  531.  
  532.   int displayWidth = imgWidth;
  533.   if (imgWidth > STRIP_LENGTH)
  534.   {
  535.     displayWidth = STRIP_LENGTH;//only display the number of led's we have
  536.   }
  537.  
  538.  
  539.   /* compute the line length */
  540.   uint32_t lineLength = imgWidth * 3;
  541.   if ((lineLength % 4) != 0)
  542.     lineLength = (lineLength / 4 + 1) * 4;
  543.  
  544.   Serial.println("Line Length");
  545.   Serial.println(lineLength,DEC);
  546.  
  547.   int x = 0;
  548.   for(int y=imgHeight;y>0 ;y--) {
  549.  
  550.     int bufpos=0;    
  551.  
  552.  
  553.     if ( dualstrip == true)
  554.     {
  555.       int pos = 0;
  556.       for(int x=0;x <((displayWidth)/2) ;x ++) {
  557.  
  558.         uint32_t offset = (MYBMP_BF_OFF_BITS + (((y-1)* lineLength) + (pos*3))) ;
  559.         //Serial.println("x = ");
  560.         //Serial.println(x,DEC);
  561.         dataFile.seek(offset);
  562.  
  563.         int g=gamma(readByte());
  564.         int b=gamma(readByte());
  565.         int r=gamma(readByte());
  566.         strip.setPixelColor(x,r,g,b);
  567.  
  568.         g=gamma(readByte());
  569.         b=gamma(readByte());
  570.         r=gamma(readByte());
  571.         strip.setPixelColor((STRIP_LENGTH-x)-1,r,b,g);
  572.         pos+=2;
  573.  
  574.       }
  575.      
  576.       latchanddelay(frameDelay/2);
  577.      
  578.  
  579.     }
  580.     else
  581.     {
  582.       for(int x=0;x <displayWidth  ;x++) {
  583.  
  584.         uint32_t offset = (MYBMP_BF_OFF_BITS + (((y-1)* lineLength) + (x*3))) ;
  585.  
  586.         dataFile.seek(offset);
  587.  
  588.         int g=gamma(readByte());
  589.         int b=gamma(readByte());
  590.         int r=gamma(readByte());
  591.         strip.setPixelColor(x,r,b,g);
  592.  
  593.  
  594.       }
  595.        latchanddelay(frameDelay);
  596.     }
  597.    
  598.  
  599.   }
  600.   Serial.println("Finished");
  601.   ClearStrip(100);
  602. }
  603.  
  604. //sort the filenames in alphabetical order
  605. void isort(String *filenames, int n)
  606. {
  607.   for (int i = 1; i < n; ++i)
  608.   {
  609.     String j = filenames[i];
  610.     int k;
  611.     for (k = i - 1; (k >= 0) && (j < filenames[k]); k--)
  612.     {
  613.       filenames[k + 1] = filenames[k];
  614.     }
  615.     filenames[k + 1] = j;
  616.   }
  617. }
  618.  
  619. // Gamma correction compensates for our eyes' nonlinear perception of
  620. // intensity.  It's the LAST step before a pixel value is stored, and
  621. // allows intermediate rendering/processing to occur in linear space.
  622. // The table contains 256 elements (8 bit input), though the outputs are
  623. // only 7 bits (0 to 127).  This is normal and intentional by design: it
  624. // allows all the rendering code to operate in the more familiar unsigned
  625. // 8-bit colorspace (used in a lot of existing graphics code), and better
  626. // preserves accuracy where repeated color blending operations occur.
  627. // Only the final end product is converted to 7 bits, the native format
  628. // for the LPD8806 LED driver.  Gamma correction and 7-bit decimation
  629. // thus occur in a single operation.
  630. PROGMEM prog_uchar gammaTable[]  = {
  631.   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  632.   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
  633.   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,
  634.   2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,
  635.   4,  4,  4,  4,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  7,  7,
  636.   7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11,
  637.   11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
  638.   16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22,
  639.   23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
  640.   30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 37, 38, 38, 39,
  641.   40, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50,
  642.   50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 58, 58, 59, 60, 61, 62,
  643.   62, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71, 72, 73, 74, 74, 75,
  644.   76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  645.   92, 93, 94, 95, 96, 97, 98, 99,100,101,102,104,105,106,107,108,
  646.   109,110,111,113,114,115,116,117,118,120,121,122,123,125,126,127
  647. };
  648.  
  649.  
  650. inline byte gamma(byte x) {
  651.   return pgm_read_byte(&gammaTable[x]);
  652. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement