Advertisement
masaakiNakamura

Lite3DP_Customized_00

Feb 25th, 2024 (edited)
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 29.40 KB | Source Code | 0 0
  1. /*
  2.  
  3.   Lite3DP S1 v1.0 - Crowd Supply campaign
  4.  
  5.   https://github.com/Lite3DP/Lite3DP-S1
  6.  
  7.   MIT License
  8.  
  9.   Please visit www.lite3dp.com for more information, documentation and software.
  10.  
  11.   email: lite3dp@lite3dp.com
  12.  
  13. */
  14.  
  15. /*
  16.   Customized
  17.   1) Menu screen is rotated 180 deg.
  18.   2) UV-LED is turned on while setup.
  19. /*
  20.  
  21. ***SELECT TO UPLOAD***
  22.  
  23. BOARD: Arduino Pro or Pro Mini
  24.  
  25. PROCESSOR: ATmega328P (5V, 16 MHz)
  26.  
  27. */
  28.  
  29.  
  30. /*
  31.  
  32.   Libraries used:
  33.  
  34.   SdFat: https://github.com/greiman/SdFat, MIT license
  35.  
  36.   Adafruit_GFX_AS and Adafruit_ILI9341_AS: https://github.com/Bodmer/TFT_ILI9341 (newest version)
  37.  
  38.   Original Adafruit library: https://github.com/adafruit/Adafruit_ILI9341
  39.  
  40. */
  41.  
  42.  
  43. /*
  44.  
  45.   This is a simple program so that anyone can make changes, upgrades and customizations.
  46.  
  47.   It is basically structured around a system of numbered "screens" for menu navigation.
  48.   Depending on the screen, the buttons will have different functions.
  49.  
  50.   The main functions are "print" and "calibrate".
  51.  
  52.   The possibility of customizing the motor, the final height and the threaded rod is contemplated ("For customization" section).
  53.  
  54.   In the configuration section you can change some values such as the lift distance and the number of initial layers.
  55.  
  56. */
  57. ////////////////////////////////////////////////////////////
  58.  
  59. // ***** FOR CUSTOMIZATION ****
  60.  
  61. // If you want to change the length of the linear guide, the threaded rod, the motor or the microsteps
  62. // change them here and the rest of the variables will be updated.
  63.  
  64.  
  65. #define Lguide 100               //Total length of linear guide (Default value : 100 mm)
  66.  
  67. #define pitch 2                  //Pitch of movement screw thread (Default value : 2 mm)
  68.  
  69. #define stepsMotorPerRev 120     //Motor steps per revolution, including gear reduction (Default value : 288 steps/rev)
  70.  
  71. #define microsteps 16             //Driver microsteps (Default value 32)
  72.  
  73.  
  74. ////////////////////////////////////////////////////////////
  75.  
  76. // **** CONFIGURATION PARAMETERS ****
  77.  
  78.  
  79. #define hUp 3                   // Lift distance in mm (Default value: 3 mm)
  80.  
  81. #define hUpInitial 5            // Inital layers lift distance in mm (Default value: 5 mm)
  82.  
  83. #define FirstLayers 5           //Number of initial layers (Default value: 5 initial layers)
  84.  
  85. int expotime = 15;              //Starting exposure time in seconds
  86.  
  87. int iexpotime = 45;             //Starting initial exposure time in seconds
  88.  
  89. #define HighSpeed 80          //Delay in microseconds of the stepper motor pulses; higher speed at lower value (Default = 80 microseconds)
  90.  
  91. #define LowSpeed 160           //Delay in microseconds of the stepper motor pulses; higher speed at lower value (Default = 160 microseconds)
  92.  
  93. #define updowntime 6.7          //Printing downtime in seconds. Experimental value used to calculate print times. Counted from turning off the UV light on one layer and turning on the UV light on the next one.
  94.                                 //It must be modified if any of the configuration parameters are changed (speeds, number of initial layers, lift distance).
  95.  
  96.  
  97. ////////////////////////////////////////////////////////////
  98.  
  99. // Pin definition
  100.  
  101. #define PinDir 15
  102. #define PinStep 14
  103. #define PinEn 10
  104.  
  105. #define sclk 13
  106. #define miso 12
  107. #define mosi 11
  108. #define cs   4
  109. #define dc   3
  110. #define rst  2
  111.  
  112. #define _sdcs 5
  113.  
  114. #define BtnUp 9
  115.  
  116. #define BtnDown 6
  117. #define BtnOK 7
  118. #define BtnCancel 8
  119.  
  120. #define PinEndStop 17
  121.  
  122. #define LightPin 16
  123.  
  124.  
  125. ///////////////////////////////////////////////
  126.  
  127. // ****LIBRARIES****
  128.  
  129. #include <Adafruit_GFX_AS.h>
  130. #include <Adafruit_ILI9341_AS.h>
  131. #include <SPI.h>
  132. #include <EEPROM.h>
  133.  
  134. #include <SdFat.h>
  135. SdFat SD;
  136.  
  137. Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(cs, dc, rst);
  138. #define BU_BMP 1 // Temporarily flip the TFT coords for standard Bottom-Up bit maps
  139. #define TD_BMP 0 // Draw inverted Top-Down bitmaps in standard coord frame
  140.  
  141.  
  142. ///////////////////////////////////////////////
  143.  
  144. // ****GLOBAL VARIABLES****
  145.  
  146. // **Screen number**
  147.  
  148. byte screen = 1;
  149.  
  150.  
  151. //**Button status**
  152.  
  153. bool edoBtnUP = HIGH;
  154. bool edoBtnDOWN = HIGH;
  155. bool edoBtnOK = HIGH;
  156. bool edoBtnCANCEL = HIGH;
  157.  
  158.  
  159. //**Button delay**
  160.  
  161. #define delaybutton 90
  162.  
  163.  
  164. // **EndStop Lecture**
  165.  
  166. bool LectEndStop;
  167.  
  168.  
  169. // **Layer Height**
  170.  
  171. float hLayer = 0.050;
  172. int hLayerx1000 = hLayer * 1000;
  173.  
  174.  
  175. // **Ascendant movement Height (mm)**
  176.  
  177. int maxheight = Lguide - 30;          //(30 mm lost due to carriage and platform)
  178.  
  179.  
  180. // **Print descendant movement**
  181.  
  182. float hDown = hUp - hLayer;
  183.  
  184. float hDownInitial = hUpInitial - hLayer;
  185.  
  186. // **Steps per mm**
  187.  
  188. int StepsPerMm = stepsMotorPerRev * microsteps / pitch;
  189.  
  190.  
  191. // **Calibration aditional steps**
  192.  
  193. #define maxAddDesc 3     // Additional maximum descent (Default = 3 mm)
  194.  
  195. int maxAddSteps = maxAddDesc * StepsPerMm / 80;  //Division by 80 is due to memory EEPROM; then it is multiplied again.
  196.  
  197. int stepsadditional = 0;
  198.  
  199. int stepsadditionalx80;
  200.  
  201.  
  202. // **For the correct name of file**
  203.  
  204. String DirAndFile;
  205.  
  206. String FileName;
  207.  
  208. char *result = malloc(5);
  209.  
  210. int number;
  211.  
  212.  
  213. //**For the layers counter**
  214.  
  215. int LayersCounter = 0;
  216.  
  217. int Layers;
  218.  
  219. String dirfoldersel;
  220.  
  221.  
  222. // **For the folder´s name**
  223.  
  224. File root;
  225.  
  226. char foldersel[13];
  227.  
  228. int counter = 1;
  229.  
  230.  
  231. ///////////////////////////////////////////////
  232.  
  233.  
  234.  
  235. void setup(void) {
  236.  
  237.  
  238.   pinMode (BtnUp, INPUT_PULLUP);
  239.   pinMode (BtnDown, INPUT_PULLUP);
  240.   pinMode (BtnCancel, INPUT_PULLUP);
  241.   pinMode (BtnOK, INPUT_PULLUP);
  242.  
  243.   pinMode (PinDir, OUTPUT);
  244.   pinMode (PinStep, OUTPUT);
  245.   pinMode (PinEn, OUTPUT);
  246.  
  247.   pinMode (LightPin, OUTPUT);
  248.  
  249.   pinMode (PinEndStop, INPUT);
  250.  
  251.  
  252.   digitalWrite (LightPin, LOW);
  253.   digitalWrite (PinEn, HIGH);
  254.  
  255.   tft.init();
  256.  
  257.   screenlite3dp();  //Opening screen
  258.  
  259.   screen1();   //First menu screen
  260.  
  261. }
  262.  
  263.  
  264. ////////////////////////////////////////////////////
  265.  
  266. void loop() {
  267.  
  268.   edoBtnUP = digitalRead (BtnUp);
  269.   edoBtnDOWN = digitalRead (BtnDown);
  270.   edoBtnOK = digitalRead (BtnOK);
  271.   edoBtnCANCEL = digitalRead (BtnCancel);
  272.  
  273.   if (edoBtnUP == LOW) {
  274.  
  275.     switch (screen) {
  276.  
  277.       case 1:
  278.         movasc(0.05, HighSpeed);    //Allows the possibility of raising the platform
  279.         break;
  280.  
  281.       case 11:
  282.         folderUp(root);
  283.         delaybtn();
  284.         break;
  285.  
  286.       case 12:
  287.         switch (hLayerx1000) {
  288.           case 50:
  289.             hLayer = 0.1;
  290.             hLayerx1000 = hLayer * 1000;
  291.             drawVariableFloat(161, 115, hLayer);
  292.             adjuststeps();
  293.             break;
  294.         }
  295.         delaybtn();
  296.         break;
  297.  
  298.  
  299.       case 13:
  300.         if (expotime >= 0 && screen == 13) {
  301.           expotime++;
  302.           tft.fillRect(122, 112, 76, 36, ILI9341_BLACK);
  303.           tft.setTextColor(ILI9341_WHITE);
  304.           drawVariableInt(161, 115, expotime);
  305.         }
  306.         iexpotime = 3 * expotime;   // initial approximation
  307.         delaybtn();
  308.         break;
  309.  
  310.       case 14:
  311.         if (iexpotime >= 0 && screen == 14) {
  312.           iexpotime++;
  313.           tft.fillRect(122, 112, 76, 36, ILI9341_BLACK);
  314.           tft.setTextColor(ILI9341_WHITE);
  315.           drawVariableInt(161, 115, iexpotime);
  316.         }
  317.         delaybtn();
  318.         break;
  319.  
  320.  
  321.       case 21:                                   //screen 21 is when the platform is down during calibration
  322.         if (stepsadditional > 0) {
  323.           stepsadditional--;
  324.           digitalWrite(PinDir, HIGH);
  325.           digitalWrite (PinEn, LOW);
  326.           for (int z = 0; z < 80; z++) {
  327.             edoBtnCANCEL = digitalRead (BtnCancel);
  328.             digitalWrite(PinStep, HIGH);
  329.             digitalWrite(PinStep, LOW);
  330.             delayMicroseconds(LowSpeed);
  331.           }
  332.           digitalWrite (PinEn, HIGH);
  333.         }
  334.         break;
  335.  
  336.     }
  337.  
  338.   }
  339.  
  340.   if (edoBtnDOWN == LOW) {
  341.  
  342.     switch (screen) {
  343.  
  344.       case 1:
  345.         break;
  346.  
  347.       case 11:
  348.         folderDown(root);
  349.         delaybtn();
  350.         break;
  351.  
  352.       case 12:
  353.         switch (hLayerx1000) {
  354.           case 100:
  355.             hLayer = 0.05;
  356.             hLayerx1000 = hLayer * 1000;
  357.             drawVariableFloat(161, 115, hLayer);
  358.             adjuststeps();
  359.             break;
  360.         }
  361.         delaybtn();
  362.         break;
  363.  
  364.       case 13:
  365.         if (expotime > 0 && screen == 13) {
  366.           expotime--;
  367.           tft.fillRect(122, 112, 76, 36, ILI9341_BLACK);
  368.           tft.setTextColor(ILI9341_WHITE);
  369.           drawVariableInt(161, 115, expotime);
  370.         }
  371.         iexpotime = 3 * expotime;   // initial approximation
  372.         delaybtn();
  373.         break;
  374.  
  375.       case 14:
  376.         if (iexpotime > 0 && screen == 14) {
  377.           iexpotime--;
  378.           tft.fillRect(122, 112, 76, 36, ILI9341_BLACK);
  379.           tft.setTextColor(ILI9341_WHITE);
  380.           drawVariableInt(161, 115, iexpotime);
  381.         }
  382.         delaybtn();
  383.         break;
  384.  
  385.       case 21:                                   //screen 21 is when the platform is down during calibration
  386.         if (stepsadditional <= maxAddSteps) {
  387.           stepsadditional++;
  388.           digitalWrite(PinDir, LOW);
  389.           digitalWrite (PinEn, LOW);
  390.           for (int z = 0; z < 80; z++) {
  391.             edoBtnCANCEL = digitalRead (BtnCancel);
  392.             digitalWrite(PinStep, HIGH);
  393.             digitalWrite(PinStep, LOW);
  394.             delayMicroseconds(LowSpeed);
  395.           }
  396.           digitalWrite (PinEn, HIGH);
  397.         }
  398.         break;
  399.  
  400.     }
  401.  
  402.   }
  403.  
  404.  
  405.   if (edoBtnOK == LOW) {
  406.  
  407.     switch (screen) {
  408.  
  409.       case 1:
  410.         blackscreen();
  411.         calibrate();
  412.         screen = 21;
  413.         break;
  414.  
  415.       case 11:
  416.         contarlayers();
  417.         if (LayersCounter > maxheight * 20 + 3) {
  418.           cleanscreen();
  419.           tft.drawCentreString("HEIGHT EXCEEDED", 160, 115, 2);
  420.           delay(1200);
  421.           screen = 11;
  422.           screen11();
  423.         }
  424.  
  425.         else {
  426.           screen = 12;
  427.           screen12();
  428.         }
  429.         break;
  430.  
  431.       case 12:
  432.         screen = 13;
  433.         screen13();
  434.         break;
  435.  
  436.       case 13:
  437.         screen = 14;
  438.         screen14();
  439.         break;
  440.  
  441.       case 14:
  442.         screen = 15;
  443.         screen15();
  444.         break;
  445.  
  446.       case 15:
  447.         switch (hLayerx1000) {
  448.  
  449.           case 50:
  450.             Layers = LayersCounter;
  451.             break;
  452.  
  453.           case 100:
  454.             Layers = LayersCounter / 2;
  455.  
  456.             int resto = Layers * 2;
  457.             resto = LayersCounter - resto;
  458.  
  459.             if (resto > 0) {
  460.               Layers++;
  461.             }
  462.  
  463.             break;
  464.         }
  465.  
  466.         screen = 16;
  467.         screen16();
  468.         break;
  469.  
  470.       case 16:
  471.         blackscreen();
  472.         print();
  473.         break;
  474.  
  475.       case 21:
  476.         EEPROM.write (11, stepsadditional);
  477.         movasc(50, HighSpeed);                    // 50 mm lift to make room for reading the display and tray entry.
  478.         if (!SD.begin(_sdcs, SPI_FULL_SPEED)) {
  479.           digitalWrite (LightPin, LOW);
  480.           cleanscreen();
  481.           tft.drawCentreString("Please", 160, 65, 2);
  482.           tft.drawCentreString("insert SD card", 160, 100, 2);
  483.           screen = 22;
  484.         }
  485.         else {
  486.           digitalWrite (LightPin, LOW);
  487.           root = SD.open("/");
  488.           screen11();
  489.           screen = 11;
  490.         }
  491.         break;
  492.  
  493.       case 22:
  494.         if (!SD.begin(_sdcs, SPI_FULL_SPEED)) {
  495.           digitalWrite (LightPin, LOW);
  496.           cleanscreen();
  497.           tft.drawCentreString("Please", 160, 65, 2);
  498.           tft.drawCentreString("insert SD card", 160, 100, 2);
  499.           screen = 22;
  500.         }
  501.         else {
  502.           digitalWrite (LightPin, LOW);
  503.           root = SD.open("/");
  504.           screen11();
  505.           screen = 11;
  506.         }
  507.         break;
  508.  
  509.  
  510.     }
  511.  
  512.     delaybtn();
  513.  
  514.   }
  515.  
  516.   if (edoBtnCANCEL == LOW) {
  517.  
  518.  
  519.     if (screen < 17 && screen > 11 ) {
  520.       screen = 11;
  521.       screen11();
  522.     }
  523.  
  524.     else {
  525.       screen = 1;
  526.       screen1();
  527.     }
  528.     delaybtn();
  529.   }
  530.  
  531.  
  532. }
  533.  
  534. ////////////////////////////////////////////////////////////
  535.  
  536.  
  537. //***** MAIN FUNCTIONS *****
  538.  
  539. // PRINT FUNCTION
  540.  
  541. void print() {
  542.  
  543.   number = 1;
  544.   buildfolder();
  545.   calibrate();
  546.  
  547.   // Printing the first layers (with initial exposure time)
  548.  
  549.   for (int l = 0; l < FirstLayers; l++) {
  550.  
  551.     printname();
  552.     digitalWrite (LightPin, HIGH);
  553.     delayprint1();              
  554.     delayprint1();
  555.     delayprint1();
  556.     delayprint1();
  557.     digitalWrite (LightPin, LOW);
  558.     blackscreen();
  559.     pause();                         //allows to enter the pause function by holding down the ESC button
  560.     movasc(hUpInitial, LowSpeed);
  561.     delay(200);
  562.     movdesc(hDownInitial, LowSpeed);
  563.  
  564.   }
  565.  
  566.   // Printing the rest
  567.  
  568.   for (int l = 0; l < Layers - FirstLayers; l++) {
  569.  
  570.     printname();
  571.     digitalWrite (LightPin, HIGH);
  572.     delayprint2();
  573.     delayprint2();
  574.     delayprint2();
  575.     delayprint2();
  576.     digitalWrite (LightPin, LOW);
  577.     blackscreen();
  578.     pause();                         //allows to enter the pause function by holding down the ESC button
  579.     movasc(hUp, LowSpeed);
  580.     delay(200);
  581.     movdesc(hDown, LowSpeed);
  582.  
  583.   }
  584.  
  585.   screen = 17;
  586.   screen17();
  587.  
  588. }
  589.  
  590. ///////////////////////
  591.  
  592. // CALIBRATION FUNCTION
  593.  
  594. void calibrate() {
  595.  
  596.   LectEndStop = digitalRead(PinEndStop);
  597.  
  598.   if (LectEndStop != HIGH) {
  599.  
  600.     desctoendstop();
  601.  
  602.     delay(600);
  603.     stepsadditional = EEPROM.read(11);
  604.     stepsadditionalx80 = stepsadditional * 80;
  605.  
  606.     digitalWrite (PinEn, LOW);
  607.     digitalWrite(PinDir, LOW);
  608.  
  609.     for (int z = 0; z < stepsadditionalx80; z++) {
  610.       edoBtnCANCEL = digitalRead (BtnCancel);
  611.       digitalWrite(PinStep, HIGH);
  612.       digitalWrite(PinStep, LOW);
  613.       delayMicroseconds(LowSpeed);
  614.       if (edoBtnCANCEL == LOW) {
  615.         break;
  616.       }
  617.     }
  618.  
  619.     digitalWrite (PinEn, HIGH);
  620.   }
  621. }
  622.  
  623. /////////////////////////////////////////////////////////////
  624.  
  625. // CALIBRATION AND PRINT SUPPORT FUNCTIONS
  626.  
  627. void folderDown(File dir) {
  628.   counter++;
  629.   for (int i = 0; i < counter; i++) {
  630.     while (true) {
  631.       File entry =  dir.openNextFile();
  632.       if (! entry) {
  633.         break;
  634.       }
  635.       if (entry.isDirectory()) {
  636.         entry.getName(foldersel, 13);
  637.         break;
  638.       }
  639.       entry.close();
  640.     }
  641.   }
  642.   tft.fillRect(78, 130, 164, 36, ILI9341_BLACK);
  643.   tft.setTextColor(ILI9341_WHITE);
  644.   tft.drawCentreString(foldersel, 161, 134, 2);
  645.   delay(200);
  646.  
  647. }
  648.  
  649. void folderUp(File dir) {
  650.   if (counter > 2) {
  651.     counter --;
  652.     for (int i = 0; i < counter; i++) {
  653.       while (true) {
  654.         File entry =  dir.openNextFile();
  655.         if (! entry) {
  656.           break;
  657.         }
  658.         if (entry.isDirectory()) {
  659.           entry.getName(foldersel, 13);
  660.           break;
  661.         }
  662.         entry.close();
  663.       }
  664.     }
  665.     tft.fillRect(78, 130, 164, 36, ILI9341_BLACK);
  666.     tft.setTextColor(ILI9341_WHITE);
  667.     tft.drawCentreString(foldersel, 161, 134, 2);
  668.     delay(200);
  669.   }
  670.  
  671. }
  672.  
  673.  
  674. void movasc (float Mm, int delaysteps) {
  675.  
  676.   long int stepsmotor = StepsPerMm * Mm;
  677.  
  678.   digitalWrite (PinEn, LOW);
  679.   digitalWrite(PinDir, HIGH);
  680.  
  681.   for (long int x = 0; x < stepsmotor; x++) {
  682.     edoBtnCANCEL = digitalRead (BtnCancel);
  683.     digitalWrite(PinStep, HIGH);
  684.     digitalWrite(PinStep, LOW);
  685.     delayMicroseconds(delaysteps);
  686.   }
  687.  
  688.   digitalWrite (PinEn, HIGH);
  689. }
  690.  
  691.  
  692. void movdesc (float Mm, int delaysteps) {
  693.  
  694.   long int stepsmotor = StepsPerMm * Mm;
  695.  
  696.   digitalWrite (PinEn, LOW);
  697.   digitalWrite(PinDir, LOW);
  698.  
  699.   for (long int x = 0; x < stepsmotor; x++) {
  700.     edoBtnCANCEL = digitalRead (BtnCancel);
  701.     digitalWrite(PinStep, HIGH);
  702.     digitalWrite(PinStep, LOW);
  703.     delayMicroseconds(delaysteps);
  704.   }
  705.   digitalWrite (PinEn, HIGH);
  706. }
  707.  
  708.  
  709. void buildfolder() {
  710.  
  711.   DirAndFile = "";
  712.   String folderselString = String(foldersel);
  713.   String barra = "/";
  714.   DirAndFile += barra;
  715.   DirAndFile += folderselString;
  716.   DirAndFile += barra;
  717.   FileName = DirAndFile;
  718.  
  719. }
  720.  
  721. void contarlayers() {
  722.  
  723.   LayersCounter = 0;
  724.   dirfoldersel = "";
  725.   String folderselString2 = String(foldersel);
  726.   number = 1;
  727.   dirfoldersel += "/";
  728.   dirfoldersel += folderselString2;
  729.   dirfoldersel += "/";
  730.   char dirfolderselChar[20];
  731.   dirfoldersel.toCharArray(dirfolderselChar, 20);
  732.   File dircarp = SD.open(dirfolderselChar);
  733.   while (true) {
  734.     File entry =  dircarp.openNextFile();
  735.     if (! entry) {
  736.       break;
  737.     }
  738.     if (entry.isDirectory()) {
  739.     } else {
  740.       LayersCounter ++;
  741.     }
  742.     entry.close();
  743.   }
  744. }
  745.  
  746.  
  747. void delayprint1() {
  748.   delay (iexpotime * 250);
  749. }
  750.  
  751.  
  752. void delayprint2() {
  753.   delay (expotime * 250);
  754. }
  755.  
  756.  
  757. void printname() {
  758.   FileName += number;
  759.   FileName += ".bmp";
  760.   char NameChar[20];
  761.   FileName.toCharArray(NameChar, 20);
  762.   tft.setRotation(1);
  763.   drawBMP(NameChar, 0, 0, BU_BMP);
  764.   switch (hLayerx1000) {
  765.     case 50:
  766.       number ++;
  767.       break;
  768.  
  769.     case 100:
  770.       number = number + 2;
  771.       break;
  772.   }
  773.   FileName = DirAndFile;
  774. }
  775.  
  776.  
  777. void pause() {
  778.  
  779.   edoBtnOK = digitalRead (BtnOK);
  780.   edoBtnCANCEL = digitalRead (BtnCancel);
  781.  
  782.   if (edoBtnCANCEL == LOW) {
  783.  
  784.     float heightActual = number * 0.05;
  785.     float heightAdd = maxheight - heightActual;
  786.     movasc(heightAdd, HighSpeed);
  787.     while (digitalRead (BtnOK) != LOW) {
  788.     }
  789.     movdesc(heightAdd, HighSpeed);
  790.     delaybtn();
  791.  
  792.   }
  793.  
  794. }
  795.  
  796.  
  797.  
  798. void desctoendstop() {
  799.  
  800.   digitalWrite (PinEn, LOW);
  801.   digitalWrite(PinDir, LOW);
  802.   LectEndStop = digitalRead(PinEndStop);
  803.  
  804.   while (LectEndStop != HIGH) {
  805.     LectEndStop = digitalRead(PinEndStop);
  806.     edoBtnCANCEL = digitalRead (BtnCancel);
  807.     digitalWrite(PinStep, HIGH);
  808.     digitalWrite(PinStep, LOW);
  809.     delayMicroseconds(HighSpeed);
  810.   }
  811.   delay(300);
  812. }
  813.  
  814.  
  815. void delaybtn() {
  816.   delay(delaybutton);
  817. }
  818.  
  819.  
  820. void adjuststeps() {
  821.  
  822.   hDown = hUp - hLayer;
  823.   hDownInitial = hUpInitial - hLayer;
  824.  
  825. }
  826.  
  827.  
  828. ////////////////////////////////////////////////////////////
  829.  
  830. // SCREENS
  831.  
  832.  
  833. void screenlite3dp() {
  834.  
  835.   tft.setRotation(1);
  836.   tft.setTextSize(2);
  837.   blackscreen();
  838.   tft.setTextColor(ILI9341_WHITE);
  839.   tft.drawCentreString("Lite3DP", 160, 100, 2);
  840.   tft.fillTriangle(123, 104, 134, 104, 129, 114, ILI9341_BLUE);
  841.   delay(2500);
  842.   digitalWrite (LightPin, HIGH);
  843. }
  844.  
  845. void screen1() {
  846.  
  847.   tft.setRotation(1);
  848.   cleanscreen();
  849.   bannerpreparation();
  850.   tft.drawCentreString("Prepare!", 160, 120, 2);
  851.   digitalWrite (LightPin, HIGH);
  852. }
  853.  
  854.  
  855. void screen11() {
  856.  
  857.   bannerprint();
  858.   cleanscreen();
  859.   tft.setTextColor(ILI9341_WHITE);
  860.   tft.drawCentreString("SELECT FILE", 160, 65, 2);
  861.   tft.drawCentreString(foldersel, 161, 134, 2);
  862.   tft.drawRect(75, 123, 170, 50, ILI9341_WHITE);
  863.   arrows();
  864.  
  865. }
  866.  
  867.  
  868. void screen12() {
  869.  
  870.   cleanscreen();
  871.   tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  872.   tft.drawCentreString("LAYER HEIGHT", 160, 60, 2);
  873.   drawVariableFloat(161, 115, hLayer);
  874.   tft.drawCentreString("mm", 161, 160, 2);
  875.   rectscreen();
  876.   arrows();
  877.  
  878. }
  879.  
  880.  
  881. void screen13() {
  882.  
  883.   cleanscreen();
  884.   tft.setTextColor(ILI9341_WHITE);
  885.   tft.drawCentreString("EXPOSURE TIME", 160, 60, 2);
  886.   drawVariableInt(161, 115, expotime);
  887.   tft.drawCentreString("sec", 161, 160, 2);
  888.   rectscreen();
  889.   arrows();
  890.  
  891. }
  892.  
  893.  
  894. void screen14() {
  895.  
  896.   cleanscreen();
  897.   tft.setTextColor(ILI9341_WHITE);
  898.   tft.drawCentreString("INITIAL EXPOSURE", 160, 60, 2);
  899.   drawVariableInt(161, 115, iexpotime);
  900.   tft.drawCentreString("sec", 161, 160, 2);
  901.   rectscreen();
  902.   arrows();
  903. }
  904.  
  905.  
  906. void screen15() {
  907.  
  908.   cleanscreen();
  909.   tft.setTextColor(ILI9341_WHITE);
  910.   tft.drawCentreString("CONFIRMATION", 160, 55, 2);
  911.  
  912.   tft.drawString("FOLDER:", 20, 100, 2);
  913.   tft.drawString(foldersel, 220, 100, 2);
  914.  
  915.   tft.drawString("LAYER H.:", 20, 130, 2);
  916.   drawVariableFloat(250, 130, hLayer);
  917.  
  918.   tft.drawString("EXP. TIME:", 20, 160, 2);
  919.   drawVariableInt2(220, 160, expotime);
  920.  
  921.   tft.drawString("INITIAL EXP.:", 20, 190, 2);
  922.   drawVariableInt2(220, 190, iexpotime);
  923.  
  924. }
  925.  
  926.  
  927. void screen16() {
  928.  
  929.   long int timelayersinitial = FirstLayers * iexpotime;
  930.   long int quantitylayersresto = Layers - FirstLayers;
  931.   long int timerestodelayers = quantitylayersresto * expotime;
  932.   long int timesubebajatot = Layers * updowntime;
  933.   long int timetotalseg = timesubebajatot + timelayersinitial + timerestodelayers;
  934.   long int timetotalmin = timetotalseg / 60;
  935.   int timetotalhours = timetotalmin / 60;
  936.   int restominutes = timetotalmin - timetotalhours * 60;
  937.  
  938.   tft.setRotation(1);
  939.   blackscreen();
  940.   tft.setTextColor(ILI9341_WHITE);
  941.   bannerprint();
  942.  
  943.   tft.drawCentreString("DETAILS", 160, 55, 2);
  944.  
  945.   tft.drawString("TOTAL LAYERS:", 14, 100, 2);
  946.   drawVariableInt2(226, 100, Layers);
  947.  
  948.   tft.drawString("DURATION:", 14, 135, 2);
  949.   drawVariableInt(175, 135, timetotalhours);
  950.   tft.drawString("h", 195, 135, 2);
  951.  
  952.   drawVariableInt(248, 135, restominutes);
  953.   tft.drawString("min", 272, 135, 2);
  954.  
  955.  
  956.   tft.drawCentreString("Insert tray and start!", 160, 190, 2);
  957.  
  958. }
  959.  
  960.  
  961. void screen17() {
  962.  
  963.   tft.setRotation(1);
  964.   blackscreen();
  965.  
  966.   int heightup = Layers * hLayer;
  967.   int heightremain = maxheight - heightup;
  968.  
  969.   movasc(heightremain, HighSpeed);
  970.  
  971.   while (digitalRead (BtnCancel) != LOW) {  //This is for led 13 to blink
  972.     blackscreen();
  973.     delay(100);
  974.   }
  975.  
  976. }
  977.  
  978. ////////////////////////////////////////////////////////////
  979.  
  980. // SCREEN SUPPORT FUNCTIONS
  981.  
  982.  
  983. void drawVariableInt(int xPos, int yPos, int vartodraw) {
  984.  
  985. String vartodrawString = String(vartodraw);
  986. char vartodrawChar[7];
  987. vartodrawString.toCharArray(vartodrawChar, 7);
  988. tft.drawCentreString(vartodrawChar, xPos, yPos, 2);
  989.  
  990. }
  991.  
  992. void drawVariableInt2(int xPos, int yPos, int vartodraw) {
  993.  
  994. String vartodrawString = String(vartodraw);
  995. char vartodrawChar[7];
  996. vartodrawString.toCharArray(vartodrawChar, 7);
  997. tft.drawString(vartodrawChar, xPos, yPos, 2);
  998.  
  999. }
  1000.  
  1001. void drawVariableFloat(int xPos, int yPos, float vartodraw) {
  1002.  
  1003. String vartodrawString = String(vartodraw);
  1004. char vartodrawChar[7];
  1005. vartodrawString.toCharArray(vartodrawChar, 7);
  1006. tft.drawCentreString(vartodrawChar, xPos, yPos, 2);
  1007.  
  1008. }
  1009.  
  1010.  
  1011.  
  1012.  
  1013. void arrows() {
  1014.  
  1015.   tft.fillTriangle(260, 135, 280, 135, 270, 120, ILI9341_WHITE );  //up arrow
  1016.   tft.fillTriangle(260, 165, 280, 165, 270, 180, ILI9341_WHITE );  //down arrow
  1017. }
  1018.  
  1019.  
  1020. void rectscreen() {
  1021.  
  1022.   tft.drawRect(120, 110, 80, 40, ILI9341_WHITE);
  1023. }
  1024.  
  1025.  
  1026. void blackscreen() {
  1027.  
  1028.   digitalWrite (LightPin, LOW);
  1029.   tft.fillScreen(ILI9341_BLACK);
  1030. }
  1031.  
  1032.  
  1033. void cleanscreen() {
  1034.   tft.fillRect(0, 40, 320, 200, ILI9341_BLACK);
  1035. }
  1036.  
  1037.  
  1038.  
  1039. void rectblue () {
  1040.  
  1041.   tft.fillRect(0, 0, 320, 40, ILI9341_BLUE);
  1042. }
  1043.  
  1044.  
  1045.  
  1046. void bannerprint() {
  1047.  
  1048.   rectblue();
  1049.   tft.setTextColor(ILI9341_WHITE);
  1050.   tft.drawCentreString("PRINT", 160, 6, 2);
  1051.   digitalWrite (LightPin, HIGH);
  1052. }
  1053.  
  1054.  
  1055. void bannerpreparation() {
  1056.  
  1057.   rectblue ();
  1058.   tft.setTextColor(ILI9341_WHITE);
  1059.   tft.drawCentreString("Lite3DP.com", 160, 6, 2);
  1060. }
  1061.  
  1062. /***************************************************************************************
  1063. ** Function name:           drawBMP
  1064. ** Descriptions:            draw a BMP format bitmap to the screen
  1065. ***************************************************************************************/
  1066.  
  1067. // This function opens a Windows Bitmap (BMP) file and
  1068. // displays it at the given coordinates.  It's sped up
  1069. // by reading many pixels worth of data at a time
  1070. // (rather than pixel by pixel).  Increasing the buffer
  1071. // size makes loading a little faster but the law of
  1072. // rapidly diminishing speed improvements applies.
  1073. // Suggest 8 minimum and 85 maximum (3 x this value is
  1074. // stored in a byte = 255/3 max!)
  1075. // A value of 8 is only ~20% slower than 24 or 48!
  1076. // Note that 5 x this value of RAM bytes will be needed
  1077. // Increasing beyond 48 gives little benefit.
  1078. // Use integral division of TFT (or typical often used image)
  1079. // width for slightly better speed to avoid short buffer purging
  1080.  
  1081. #define BUFF_SIZE 80
  1082.  
  1083. void drawBMP(char *filename, int x, int y, boolean flip) {
  1084.   if ((x >= tft.width()) || (y >= tft.height())) return;
  1085.   File     bmpFile;
  1086.   int16_t  bmpWidth, bmpHeight;   // Image W+H in pixels
  1087.   //uint8_t  bmpDepth;            // Bit depth (must be 24) but we dont use this
  1088.   uint32_t bmpImageoffset;        // Start address of image data in file
  1089.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  1090.   uint8_t  sdbuffer[3 * BUFF_SIZE];    // SD read pixel buffer (8 bits each R+G+B per pixel)
  1091.   uint16_t tftbuffer[BUFF_SIZE];       // TFT pixel out buffer (16-bit per pixel)
  1092.   uint8_t  sd_ptr = sizeof(sdbuffer); // sdbuffer pointer (so BUFF_SIZE must be less than 86)
  1093.   boolean  goodBmp = false;            // Flag set to true on valid header parse
  1094.   int16_t  w, h, row, col;             // to store width, height, row and column
  1095.   //uint8_t  r, g, b;   // brg encoding line concatenated for speed so not used
  1096.   uint8_t rotation;     // to restore rotation
  1097.   uint8_t  tft_ptr = 0;  // buffer pointer
  1098.  
  1099.   // Check file exists and open it
  1100.   if ((bmpFile = SD.open(filename)) == NULL) {
  1101.     //  Serial.println(F("File not found")); // Can comment out if not needed
  1102.     return;
  1103.   }
  1104.  
  1105.  
  1106.   // Parse BMP header to get the information we need
  1107.   if (read16(bmpFile) == 0x4D42) { // BMP file start signature check
  1108.     read32(bmpFile);       // Dummy read to throw away and move on
  1109.     read32(bmpFile);       // Read & ignore creator bytes
  1110.     bmpImageoffset = read32(bmpFile); // Start of image data
  1111.     read32(bmpFile);       // Dummy read to throw away and move on
  1112.     bmpWidth  = read32(bmpFile);  // Image width
  1113.     bmpHeight = read32(bmpFile);  // Image height
  1114.  
  1115.     //if (read16(bmpFile) == 1) { // Number of image planes -- must be '1'
  1116.     // Only proceed if we pass a bitmap file check
  1117.     if ((read16(bmpFile) == 1) && (read16(bmpFile) == 24) && (read32(bmpFile) == 0)) { // Must be depth 24 and 0 (uncompressed format)
  1118.       //goodBmp = true; // Supported BMP format -- proceed!
  1119.       // BMP rows are padded (if needed) to 4-byte boundary
  1120.       rowSize = (bmpWidth * 3 + 3) & ~3;
  1121.       // Crop area to be loaded
  1122.       w = bmpWidth;
  1123.       h = bmpHeight;
  1124.  
  1125.       // We might need to alter rotation to avoid tedious pointer manipulation
  1126.       // Save the current value so we can restore it later
  1127.       rotation = tft.getRotation();
  1128.       // Use TFT SGRAM coord rotation if flip is set for 25% faster rendering
  1129.       if (flip) tft.setRotation((rotation + (flip << 2)) % 8); // Value 0-3 mapped to 4-7
  1130.  
  1131.       // We might need to flip and calculate new y plot coordinate
  1132.       // relative to top left corner as well...
  1133.       switch (rotation) {
  1134.         case 0:
  1135.           if (flip) y = tft.height() - y - h; break;
  1136.         case 1:
  1137.           y = tft.height() - y - h; break;
  1138.           break;
  1139.         case 2:
  1140.           if (flip) y = tft.height() - y - h; break;
  1141.           break;
  1142.         case 3:
  1143.           y = tft.height() - y - h; break;
  1144.           break;
  1145.       }
  1146.  
  1147.       // Set TFT address window to image bounds
  1148.       // Currently, image will not draw or will be corrputed if it does not fit
  1149.       // TODO -> efficient clipping, I don't need it to be idiot proof ;-)
  1150.       tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
  1151.  
  1152.       // Finally we are ready to send rows of pixels, writing like this avoids slow 32 bit multiply
  1153.       for (uint32_t pos = bmpImageoffset; pos < bmpImageoffset + h * rowSize ; pos += rowSize) {
  1154.         // Seek if we need to on boundaries and arrange to dump buffer and start again
  1155.         if (bmpFile.position() != pos) {
  1156.           bmpFile.seek(pos);
  1157.           sd_ptr = sizeof(sdbuffer);
  1158.         }
  1159.  
  1160.         // Fill the pixel buffer and plot
  1161.         for (col = 0; col < w; col++) { // For each column...
  1162.           // Time to read more pixel data?
  1163.           if (sd_ptr >= sizeof(sdbuffer)) {
  1164.             // Push tft buffer to the display
  1165.             if (tft_ptr) {
  1166.               // Here we are sending a uint16_t array to the function
  1167.               tft.pushColors(tftbuffer, tft_ptr);
  1168.               tft_ptr = 0; // tft_ptr and sd_ptr are not always in sync...
  1169.             }
  1170.             // Finally reading bytes from SD Card
  1171.             bmpFile.read(sdbuffer, sizeof(sdbuffer));
  1172.             sd_ptr = 0; // Set buffer index to start
  1173.           }
  1174.           // Convert pixel from BMP 8+8+8 format to TFT compatible 16 bit word
  1175.           // Blue 5 bits, green 6 bits and red 5 bits (16 bits total)
  1176.           // Is is a long line but it is faster than calling a library fn for this
  1177.           tftbuffer[tft_ptr++] = (sdbuffer[sd_ptr++] >> 3) | ((sdbuffer[sd_ptr++] & 0xFC) << 3) | ((sdbuffer[sd_ptr++] & 0xF8) << 8);
  1178.         } // Next row
  1179.       }   // All rows done
  1180.  
  1181.       // Write any partially full buffer to TFT
  1182.       if (tft_ptr) tft.pushColors(tftbuffer, tft_ptr);
  1183.     } // End of bitmap access
  1184.   }   // End of bitmap file check
  1185.   //}     // We can close the file now
  1186.  
  1187.   bmpFile.close();
  1188.   //if(!goodBmp) Serial.println(F("BMP format not recognized."));
  1189.   tft.setRotation(rotation); // Put back original rotation
  1190. }
  1191.  
  1192. /***************************************************************************************
  1193. ** Function name:           Support functions for drawBMP()
  1194. ** Descriptions:            Read 16- and 32-bit types from the SD card file
  1195. ***************************************************************************************/
  1196.  
  1197. // BMP data is stored little-endian, Arduino is little-endian too.
  1198. // May need to reverse subscript order if porting elsewhere.
  1199.  
  1200. uint16_t read16(File & f) {
  1201.   uint16_t result;
  1202.   ((uint8_t *)&result)[0] = f.read(); // LSB
  1203.   ((uint8_t *)&result)[1] = f.read(); // MSB
  1204.   return result;
  1205. }
  1206.  
  1207. uint32_t read32(File & f) {
  1208.   uint32_t result;
  1209.   ((uint8_t *)&result)[0] = f.read(); // LSB
  1210.   ((uint8_t *)&result)[1] = f.read();
  1211.   ((uint8_t *)&result)[2] = f.read();
  1212.   ((uint8_t *)&result)[3] = f.read(); // MSB
  1213.   return result;
  1214. }
  1215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement