Advertisement
Guest User

DLW WS8201 + SD + LCD

a guest
Dec 4th, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.86 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.       entry.close();
  373.       break;
  374.     }
  375.     else
  376.     {
  377.       if (entry.isDirectory()) {
  378.         //GetNextFileName(root);
  379.       }
  380.       else {
  381.         CurrentFilename = entry.name();
  382.         if (CurrentFilename.endsWith(".bmp") || CurrentFilename.endsWith(".BMP") )//find files with our extension only
  383.         {
  384.           m_FileNames[fileCount] = entry.name();
  385.           fileCount++;
  386.         }
  387.       }
  388.     }
  389.     entry.close();
  390.   }
  391. }
  392.  
  393.  
  394. void latchanddelay(int dur)
  395. {
  396.   strip.show();
  397.   delay(dur);
  398. }
  399.  
  400. void ClearStrip(int duration)
  401. {
  402.   int x;
  403.   for(x=0;x<STRIP_LENGTH;x++)
  404.   {
  405.     strip.setPixelColor(x, 0);
  406.   }
  407.   strip.show();// Had to add this extra show, otherwise if you were displaying an image
  408.   // and the last line / pixel were white, a green pixel would remain lit!
  409.   latchanddelay(duration);
  410.  
  411. }
  412.  
  413. uint32_t readLong()
  414. {
  415.   uint32_t retValue;
  416.   byte incomingbyte;
  417.  
  418.   incomingbyte=readByte();
  419.   retValue=(uint32_t)((byte)incomingbyte);
  420.  
  421.   incomingbyte=readByte();
  422.   retValue+=(uint32_t)((byte)incomingbyte)<<8;
  423.  
  424.   incomingbyte=readByte();
  425.   retValue+=(uint32_t)((byte)incomingbyte)<<16;
  426.  
  427.   incomingbyte=readByte();
  428.   retValue+=(uint32_t)((byte)incomingbyte)<<24;
  429.  
  430.   return retValue;
  431. }
  432.  
  433. uint16_t readInt()
  434. {
  435.   byte incomingbyte;
  436.   uint16_t retValue;
  437.  
  438.   incomingbyte=readByte();
  439.   retValue+=(uint16_t)((byte)incomingbyte);
  440.  
  441.   incomingbyte=readByte();
  442.   retValue+=(uint16_t)((byte)incomingbyte)<<8;
  443.  
  444.   return retValue;
  445. }
  446.  
  447. int readByte()
  448. {
  449.   int retbyte=-1;
  450.   while(retbyte<0) retbyte= dataFile.read();
  451.   return retbyte;
  452. }
  453.  
  454.  
  455. void ReadTheFile()
  456. {
  457. #define MYBMP_BF_TYPE           0x4D42
  458. #define MYBMP_BF_OFF_BITS       54
  459. #define MYBMP_BI_SIZE           40
  460. #define MYBMP_BI_RGB            0L
  461. #define MYBMP_BI_RLE8           1L
  462. #define MYBMP_BI_RLE4           2L
  463. #define MYBMP_BI_BITFIELDS      3L
  464.  
  465.  
  466.  
  467.   uint16_t bmpType = readInt();
  468.   uint32_t bmpSize = readLong();
  469.   uint16_t bmpReserved1 = readInt();
  470.   uint16_t bmpReserved2 = readInt();
  471.   uint32_t bmpOffBits = readLong();
  472.   bmpOffBits = 54;
  473.  
  474.   Serial.println("bmpType = ");
  475.   Serial.println(bmpType,HEX);
  476.  
  477.   Serial.println("bmpSize");
  478.   Serial.println(bmpSize,DEC);
  479.  
  480.   Serial.println("bmpReserved1");
  481.   Serial.println(bmpReserved1,DEC);
  482.  
  483.   Serial.println("bmpReserved2");
  484.   Serial.println(bmpReserved2,DEC);
  485.  
  486.  
  487.   Serial.println("bmpOffBits");
  488.   Serial.println(bmpOffBits,DEC);
  489.  
  490.   /* Check file header */
  491.   if (bmpType != MYBMP_BF_TYPE || bmpOffBits != MYBMP_BF_OFF_BITS)
  492.   {
  493.     lcd.setCursor(0, 0);
  494.     lcd.print("not a bitmap");
  495.     delay(1000);
  496.     return;
  497.   }
  498.  
  499.   /* Read info header */
  500.   uint32_t imgSize = readLong();
  501.   uint32_t imgWidth = readLong();
  502.   uint32_t imgHeight = readLong();
  503.   uint16_t imgPlanes = readInt();
  504.   uint16_t imgBitCount = readInt();
  505.   uint32_t imgCompression = readLong();
  506.   uint32_t imgSizeImage = readLong();
  507.   uint32_t imgXPelsPerMeter = readLong();
  508.   uint32_t imgYPelsPerMeter = readLong();
  509.   uint32_t imgClrUsed = readLong();
  510.   uint32_t imgClrImportant = readLong();
  511.  
  512.  
  513.   Serial.println("bitmap height");
  514.   Serial.println(imgHeight,DEC);
  515.   Serial.println("bitmap Width");
  516.   Serial.println(imgWidth,DEC);
  517.   Serial.println("bitmap bpp");
  518.   Serial.println(imgBitCount,DEC);
  519.  
  520.   /* Check info header */
  521.   if( imgSize != MYBMP_BI_SIZE || imgWidth <= 0 ||
  522.     imgHeight <= 0 || imgPlanes != 1 ||
  523.     imgBitCount != 24 || imgCompression != MYBMP_BI_RGB ||
  524.     imgSizeImage == 0 )
  525.   {
  526.     lcd.setCursor(0, 0);
  527.     lcd.print("Unsupported");
  528.     lcd.setCursor(0, 1);
  529.     lcd.print("Bitmap Use 24bpp");
  530.     delay(1000);
  531.     return;
  532.   }
  533.  
  534.   int displayWidth = imgWidth;
  535.   if (imgWidth > STRIP_LENGTH)
  536.   {
  537.     displayWidth = STRIP_LENGTH;//only display the number of led's we have
  538.   }
  539.  
  540.  
  541.   /* compute the line length */
  542.   uint32_t lineLength = imgWidth * 3;
  543.   if ((lineLength % 4) != 0)
  544.     lineLength = (lineLength / 4 + 1) * 4;
  545.  
  546.   Serial.println("Line Length");
  547.   Serial.println(lineLength,DEC);
  548.  
  549.   int x = 0;
  550.   for(int y=imgHeight;y>0 ;y--) {
  551.  
  552.     int bufpos=0;    
  553.  
  554.  
  555.     if ( dualstrip == true)
  556.     {
  557.       int pos = 0;
  558.       for(int x=0;x <((displayWidth)/2) ;x ++) {
  559.  
  560.         uint32_t offset = (MYBMP_BF_OFF_BITS + (((y-1)* lineLength) + (pos*3))) ;
  561.         //Serial.println("x = ");
  562.         //Serial.println(x,DEC);
  563.         dataFile.seek(offset);
  564.  
  565.         int g=gamma(readByte());
  566.         int b=gamma(readByte());
  567.         int r=gamma(readByte());
  568.         strip.setPixelColor(x,r,g,b);
  569.  
  570.         g=gamma(readByte());
  571.         b=gamma(readByte());
  572.         r=gamma(readByte());
  573.         strip.setPixelColor((STRIP_LENGTH-x)-1,r,b,g);
  574.         pos+=2;
  575.  
  576.       }
  577.      
  578.       latchanddelay(frameDelay/2);
  579.      
  580.  
  581.     }
  582.     else
  583.     {
  584.       for(int x=0;x <displayWidth  ;x++) {
  585.  
  586.         uint32_t offset = (MYBMP_BF_OFF_BITS + (((y-1)* lineLength) + (x*3))) ;
  587.  
  588.         dataFile.seek(offset);
  589.  
  590.         int g=gamma(readByte());
  591.         int b=gamma(readByte());
  592.         int r=gamma(readByte());
  593.         strip.setPixelColor(x,r,b,g);
  594.  
  595.  
  596.       }
  597.        latchanddelay(frameDelay);
  598.     }
  599.    
  600.  
  601.   }
  602.   Serial.println("Finished");
  603.   ClearStrip(100);
  604. }
  605.  
  606. //sort the filenames in alphabetical order
  607. void isort(String *filenames, int n)
  608. {
  609.   for (int i = 1; i < n; ++i)
  610.   {
  611.     String j = filenames[i];
  612.     int k;
  613.     for (k = i - 1; (k >= 0) && (j < filenames[k]); k--)
  614.     {
  615.       filenames[k + 1] = filenames[k];
  616.     }
  617.     filenames[k + 1] = j;
  618.   }
  619. }
  620.  
  621. // Gamma correction compensates for our eyes' nonlinear perception of
  622. // intensity.  It's the LAST step before a pixel value is stored, and
  623. // allows intermediate rendering/processing to occur in linear space.
  624. // The table contains 256 elements (8 bit input), though the outputs are
  625. // only 7 bits (0 to 127).  This is normal and intentional by design: it
  626. // allows all the rendering code to operate in the more familiar unsigned
  627. // 8-bit colorspace (used in a lot of existing graphics code), and better
  628. // preserves accuracy where repeated color blending operations occur.
  629. // Only the final end product is converted to 7 bits, the native format
  630. // for the LPD8806 LED driver.  Gamma correction and 7-bit decimation
  631. // thus occur in a single operation.
  632. PROGMEM prog_uchar gammaTable[]  = {
  633.   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  634.   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
  635.   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,
  636.   2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,
  637.   4,  4,  4,  4,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  7,  7,
  638.   7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11,
  639.   11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
  640.   16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22,
  641.   23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
  642.   30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 37, 38, 38, 39,
  643.   40, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50,
  644.   50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 58, 58, 59, 60, 61, 62,
  645.   62, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71, 72, 73, 74, 74, 75,
  646.   76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  647.   92, 93, 94, 95, 96, 97, 98, 99,100,101,102,104,105,106,107,108,
  648.   109,110,111,113,114,115,116,117,118,120,121,122,123,125,126,127
  649. };
  650.  
  651.  
  652. inline byte gamma(byte x) {
  653.   return pgm_read_byte(&gammaTable[x]);
  654. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement