Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Digital Light Wand + LCD + SD + LPD8806 + BMP direct + Gamma

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