Advertisement
Guest User

DLW+SD+BMP+DUAL LPD8806

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