My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Marlin.pde 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "planner.h"
  26. #include "stepper.h"
  27. #include "temperature.h"
  28. #include "motion_control.h"
  29. #include "cardreader.h"
  30. #include "watchdog.h"
  31. #include "EEPROMwrite.h"
  32. #include "language.h"
  33. #include "pins_arduino.h"
  34. #define VERSION_STRING "1.0.0 RC2"
  35. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  36. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  37. //Implemented Codes
  38. //-------------------
  39. // G0 -> G1
  40. // G1 - Coordinated Movement X Y Z E
  41. // G2 - CW ARC
  42. // G3 - CCW ARC
  43. // G4 - Dwell S<seconds> or P<milliseconds>
  44. // G10 - retract filament according to settings of M207
  45. // G11 - retract recover filament according to settings of M208
  46. // G28 - Home all Axis
  47. // G90 - Use Absolute Coordinates
  48. // G91 - Use Relative Coordinates
  49. // G92 - Set current position to cordinates given
  50. //RepRap M Codes
  51. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  52. // M1 - Same as M0
  53. // M104 - Set extruder target temp
  54. // M105 - Read current temp
  55. // M106 - Fan on
  56. // M107 - Fan off
  57. // M109 - Wait for extruder current temp to reach target temp.
  58. // M114 - Display current position
  59. //Custom M Codes
  60. // M17 - Enable/Power all stepper motors
  61. // M18 - Disable all stepper motors; same as M84
  62. // M20 - List SD card
  63. // M21 - Init SD card
  64. // M22 - Release SD card
  65. // M23 - Select SD file (M23 filename.g)
  66. // M24 - Start/resume SD print
  67. // M25 - Pause SD print
  68. // M26 - Set SD position in bytes (M26 S12345)
  69. // M27 - Report SD print status
  70. // M28 - Start SD write (M28 filename.g)
  71. // M29 - Stop SD write
  72. // M30 - Delete file from SD (M30 filename.g)
  73. // M31 - Output time since last M109 or SD card start to serial
  74. // M42 - Change pin status via gcode
  75. // M80 - Turn on Power Supply
  76. // M81 - Turn off Power Supply
  77. // M82 - Set E codes absolute (default)
  78. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  79. // M84 - Disable steppers until next move,
  80. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  81. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  82. // M92 - Set axis_steps_per_unit - same syntax as G92
  83. // M114 - Output current position to serial port
  84. // M115 - Capabilities string
  85. // M117 - display message
  86. // M119 - Output Endstop status to serial port
  87. // M140 - Set bed target temp
  88. // M190 - Wait for bed current temp to reach target temp.
  89. // M200 - Set filament diameter
  90. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  91. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  92. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  93. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  94. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  95. // M206 - set additional homeing offset
  96. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  97. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  98. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  99. // M220 S<factor in percent>- set speed factor override percentage
  100. // M221 S<factor in percent>- set extrude factor override percentage
  101. // M240 - Trigger a camera to take a photograph
  102. // M301 - Set PID parameters P I and D
  103. // M302 - Allow cold extrudes
  104. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  105. // M400 - Finish all moves
  106. // M500 - stores paramters in EEPROM
  107. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  108. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  109. // M503 - print the current settings (from memory not from eeprom)
  110. // M999 - Restart after being stopped by error
  111. //Stepper Movement Variables
  112. //===========================================================================
  113. //=============================imported variables============================
  114. //===========================================================================
  115. //===========================================================================
  116. //=============================public variables=============================
  117. //===========================================================================
  118. #ifdef SDSUPPORT
  119. CardReader card;
  120. #endif
  121. float homing_feedrate[] = HOMING_FEEDRATE;
  122. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  123. volatile int feedmultiply=100; //100->1 200->2
  124. int saved_feedmultiply;
  125. volatile bool feedmultiplychanged=false;
  126. volatile int extrudemultiply=100; //100->1 200->2
  127. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  128. float add_homeing[3]={0,0,0};
  129. uint8_t active_extruder = 0;
  130. unsigned char FanSpeed=0;
  131. #ifdef FWRETRACT
  132. bool autoretract_enabled=true;
  133. bool retracted=false;
  134. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  135. float retract_recover_length=0, retract_recover_feedrate=8*60;
  136. #endif
  137. //===========================================================================
  138. //=============================private variables=============================
  139. //===========================================================================
  140. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  141. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  142. static float offset[3] = {0.0, 0.0, 0.0};
  143. static bool home_all_axis = true;
  144. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  145. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  146. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  147. static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  148. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  149. static bool fromsd[BUFSIZE];
  150. static int bufindr = 0;
  151. static int bufindw = 0;
  152. static int buflen = 0;
  153. //static int i = 0;
  154. static char serial_char;
  155. static int serial_count = 0;
  156. static boolean comment_mode = false;
  157. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  158. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  159. //static float tt = 0;
  160. //static float bt = 0;
  161. //Inactivity shutdown variables
  162. static unsigned long previous_millis_cmd = 0;
  163. static unsigned long max_inactive_time = 0;
  164. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  165. static unsigned long starttime=0;
  166. static unsigned long stoptime=0;
  167. static uint8_t tmp_extruder;
  168. bool Stopped=false;
  169. //===========================================================================
  170. //=============================ROUTINES=============================
  171. //===========================================================================
  172. void get_arc_coordinates();
  173. extern "C"{
  174. extern unsigned int __bss_end;
  175. extern unsigned int __heap_start;
  176. extern void *__brkval;
  177. int freeMemory() {
  178. int free_memory;
  179. if((int)__brkval == 0)
  180. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  181. else
  182. free_memory = ((int)&free_memory) - ((int)__brkval);
  183. return free_memory;
  184. }
  185. }
  186. //adds an command to the main command buffer
  187. //thats really done in a non-safe way.
  188. //needs overworking someday
  189. void enquecommand(const char *cmd)
  190. {
  191. if(buflen < BUFSIZE)
  192. {
  193. //this is dangerous if a mixing of serial and this happsens
  194. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  195. SERIAL_ECHO_START;
  196. SERIAL_ECHOPGM("enqueing \"");
  197. SERIAL_ECHO(cmdbuffer[bufindw]);
  198. SERIAL_ECHOLNPGM("\"");
  199. bufindw= (bufindw + 1)%BUFSIZE;
  200. buflen += 1;
  201. }
  202. }
  203. void setup_photpin()
  204. {
  205. #ifdef PHOTOGRAPH_PIN
  206. #if (PHOTOGRAPH_PIN > -1)
  207. SET_OUTPUT(PHOTOGRAPH_PIN);
  208. WRITE(PHOTOGRAPH_PIN, LOW);
  209. #endif
  210. #endif
  211. }
  212. void setup_powerhold()
  213. {
  214. #ifdef SUICIDE_PIN
  215. #if (SUICIDE_PIN> -1)
  216. SET_OUTPUT(SUICIDE_PIN);
  217. WRITE(SUICIDE_PIN, HIGH);
  218. #endif
  219. #endif
  220. }
  221. void suicide()
  222. {
  223. #ifdef SUICIDE_PIN
  224. #if (SUICIDE_PIN> -1)
  225. SET_OUTPUT(SUICIDE_PIN);
  226. WRITE(SUICIDE_PIN, LOW);
  227. #endif
  228. #endif
  229. }
  230. void setup()
  231. {
  232. setup_powerhold();
  233. MYSERIAL.begin(BAUDRATE);
  234. SERIAL_PROTOCOLLNPGM("start");
  235. SERIAL_ECHO_START;
  236. // Check startup - does nothing if bootloader sets MCUSR to 0
  237. byte mcu = MCUSR;
  238. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  239. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  240. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  241. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  242. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  243. MCUSR=0;
  244. SERIAL_ECHOPGM(MSG_MARLIN);
  245. SERIAL_ECHOLNPGM(VERSION_STRING);
  246. #ifdef STRING_VERSION_CONFIG_H
  247. #ifdef STRING_CONFIG_H_AUTHOR
  248. SERIAL_ECHO_START;
  249. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  250. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  251. SERIAL_ECHOPGM(MSG_AUTHOR);
  252. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  253. #endif
  254. #endif
  255. SERIAL_ECHO_START;
  256. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  257. SERIAL_ECHO(freeMemory());
  258. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  259. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  260. for(int8_t i = 0; i < BUFSIZE; i++)
  261. {
  262. fromsd[i] = false;
  263. }
  264. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  265. for(int8_t i=0; i < NUM_AXIS; i++)
  266. {
  267. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  268. }
  269. tp_init(); // Initialize temperature loop
  270. plan_init(); // Initialize planner;
  271. st_init(); // Initialize stepper;
  272. wd_init();
  273. setup_photpin();
  274. LCD_INIT;
  275. }
  276. void loop()
  277. {
  278. if(buflen < (BUFSIZE-1))
  279. get_command();
  280. #ifdef SDSUPPORT
  281. card.checkautostart(false);
  282. #endif
  283. if(buflen)
  284. {
  285. #ifdef SDSUPPORT
  286. if(card.saving)
  287. {
  288. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  289. {
  290. card.write_command(cmdbuffer[bufindr]);
  291. SERIAL_PROTOCOLLNPGM(MSG_OK);
  292. }
  293. else
  294. {
  295. card.closefile();
  296. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  297. }
  298. }
  299. else
  300. {
  301. process_commands();
  302. }
  303. #else
  304. process_commands();
  305. #endif //SDSUPPORT
  306. buflen = (buflen-1);
  307. bufindr = (bufindr + 1)%BUFSIZE;
  308. }
  309. //check heater every n milliseconds
  310. manage_heater();
  311. manage_inactivity(1);
  312. checkHitEndstops();
  313. LCD_STATUS;
  314. }
  315. void get_command()
  316. {
  317. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  318. serial_char = MYSERIAL.read();
  319. if(serial_char == '\n' ||
  320. serial_char == '\r' ||
  321. (serial_char == ':' && comment_mode == false) ||
  322. serial_count >= (MAX_CMD_SIZE - 1) )
  323. {
  324. if(!serial_count) { //if empty line
  325. comment_mode = false; //for new command
  326. return;
  327. }
  328. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  329. if(!comment_mode){
  330. comment_mode = false; //for new command
  331. fromsd[bufindw] = false;
  332. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  333. {
  334. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  335. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  336. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  337. SERIAL_ERROR_START;
  338. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  339. SERIAL_ERRORLN(gcode_LastN);
  340. //Serial.println(gcode_N);
  341. FlushSerialRequestResend();
  342. serial_count = 0;
  343. return;
  344. }
  345. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  346. {
  347. byte checksum = 0;
  348. byte count = 0;
  349. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  350. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  351. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  352. SERIAL_ERROR_START;
  353. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  354. SERIAL_ERRORLN(gcode_LastN);
  355. FlushSerialRequestResend();
  356. serial_count = 0;
  357. return;
  358. }
  359. //if no errors, continue parsing
  360. }
  361. else
  362. {
  363. SERIAL_ERROR_START;
  364. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  365. SERIAL_ERRORLN(gcode_LastN);
  366. FlushSerialRequestResend();
  367. serial_count = 0;
  368. return;
  369. }
  370. gcode_LastN = gcode_N;
  371. //if no errors, continue parsing
  372. }
  373. else // if we don't receive 'N' but still see '*'
  374. {
  375. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  376. {
  377. SERIAL_ERROR_START;
  378. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  379. SERIAL_ERRORLN(gcode_LastN);
  380. serial_count = 0;
  381. return;
  382. }
  383. }
  384. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  385. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  386. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  387. case 0:
  388. case 1:
  389. case 2:
  390. case 3:
  391. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  392. #ifdef SDSUPPORT
  393. if(card.saving)
  394. break;
  395. #endif //SDSUPPORT
  396. SERIAL_PROTOCOLLNPGM(MSG_OK);
  397. }
  398. else {
  399. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  400. LCD_MESSAGEPGM(MSG_STOPPED);
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. bufindw = (bufindw + 1)%BUFSIZE;
  408. buflen += 1;
  409. }
  410. serial_count = 0; //clear buffer
  411. }
  412. else
  413. {
  414. if(serial_char == ';') comment_mode = true;
  415. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  416. }
  417. }
  418. #ifdef SDSUPPORT
  419. if(!card.sdprinting || serial_count!=0){
  420. return;
  421. }
  422. while( !card.eof() && buflen < BUFSIZE) {
  423. int16_t n=card.get();
  424. serial_char = (char)n;
  425. if(serial_char == '\n' ||
  426. serial_char == '\r' ||
  427. (serial_char == ':' && comment_mode == false) ||
  428. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  429. {
  430. if(card.eof()){
  431. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  432. stoptime=millis();
  433. char time[30];
  434. unsigned long t=(stoptime-starttime)/1000;
  435. int sec,min;
  436. min=t/60;
  437. sec=t%60;
  438. sprintf(time,"%i min, %i sec",min,sec);
  439. SERIAL_ECHO_START;
  440. SERIAL_ECHOLN(time);
  441. LCD_MESSAGE(time);
  442. card.printingHasFinished();
  443. card.checkautostart(true);
  444. }
  445. if(!serial_count)
  446. {
  447. comment_mode = false; //for new command
  448. return; //if empty line
  449. }
  450. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  451. // if(!comment_mode){
  452. fromsd[bufindw] = true;
  453. buflen += 1;
  454. bufindw = (bufindw + 1)%BUFSIZE;
  455. // }
  456. comment_mode = false; //for new command
  457. serial_count = 0; //clear buffer
  458. }
  459. else
  460. {
  461. if(serial_char == ';') comment_mode = true;
  462. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  463. }
  464. }
  465. #endif //SDSUPPORT
  466. }
  467. float code_value()
  468. {
  469. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  470. }
  471. long code_value_long()
  472. {
  473. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  474. }
  475. bool code_seen(char code_string[]) //Return True if the string was found
  476. {
  477. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  478. }
  479. bool code_seen(char code)
  480. {
  481. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  482. return (strchr_pointer != NULL); //Return True if a character was found
  483. }
  484. #define HOMEAXIS(LETTER) \
  485. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  486. { \
  487. current_position[LETTER##_AXIS] = 0; \
  488. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  489. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  490. feedrate = homing_feedrate[LETTER##_AXIS]; \
  491. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  492. st_synchronize();\
  493. \
  494. current_position[LETTER##_AXIS] = 0;\
  495. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  496. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  497. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  498. st_synchronize();\
  499. \
  500. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  501. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  502. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  503. st_synchronize();\
  504. \
  505. current_position[LETTER##_AXIS] = LETTER##_HOME_POS;\
  506. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
  507. feedrate = 0.0;\
  508. endstops_hit_on_purpose();\
  509. }
  510. void process_commands()
  511. {
  512. unsigned long codenum; //throw away variable
  513. char *starpos = NULL;
  514. if(code_seen('G'))
  515. {
  516. switch((int)code_value())
  517. {
  518. case 0: // G0 -> G1
  519. case 1: // G1
  520. if(Stopped == false) {
  521. get_coordinates(); // For X Y Z E F
  522. prepare_move();
  523. //ClearToSend();
  524. return;
  525. }
  526. //break;
  527. case 2: // G2 - CW ARC
  528. if(Stopped == false) {
  529. get_arc_coordinates();
  530. prepare_arc_move(true);
  531. return;
  532. }
  533. case 3: // G3 - CCW ARC
  534. if(Stopped == false) {
  535. get_arc_coordinates();
  536. prepare_arc_move(false);
  537. return;
  538. }
  539. case 4: // G4 dwell
  540. LCD_MESSAGEPGM(MSG_DWELL);
  541. codenum = 0;
  542. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  543. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  544. st_synchronize();
  545. codenum += millis(); // keep track of when we started waiting
  546. previous_millis_cmd = millis();
  547. while(millis() < codenum ){
  548. manage_heater();
  549. manage_inactivity(1);
  550. LCD_STATUS;
  551. }
  552. break;
  553. #ifdef FWRETRACT
  554. case 10: // G10 retract
  555. if(!retracted)
  556. {
  557. destination[X_AXIS]=current_position[X_AXIS];
  558. destination[Y_AXIS]=current_position[Y_AXIS];
  559. destination[Z_AXIS]=current_position[Z_AXIS];
  560. current_position[Z_AXIS]+=-retract_zlift;
  561. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  562. feedrate=retract_feedrate;
  563. retracted=true;
  564. prepare_move();
  565. }
  566. break;
  567. case 11: // G10 retract_recover
  568. if(!retracted)
  569. {
  570. destination[X_AXIS]=current_position[X_AXIS];
  571. destination[Y_AXIS]=current_position[Y_AXIS];
  572. destination[Z_AXIS]=current_position[Z_AXIS];
  573. current_position[Z_AXIS]+=retract_zlift;
  574. current_position[E_AXIS]+=-retract_recover_length;
  575. feedrate=retract_recover_feedrate;
  576. retracted=false;
  577. prepare_move();
  578. }
  579. break;
  580. #endif //FWRETRACT
  581. case 28: //G28 Home all Axis one at a time
  582. saved_feedrate = feedrate;
  583. saved_feedmultiply = feedmultiply;
  584. feedmultiply = 100;
  585. previous_millis_cmd = millis();
  586. enable_endstops(true);
  587. for(int8_t i=0; i < NUM_AXIS; i++) {
  588. destination[i] = current_position[i];
  589. }
  590. feedrate = 0.0;
  591. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  592. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  593. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  594. HOMEAXIS(Z);
  595. }
  596. #endif
  597. #ifdef QUICK_HOME
  598. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  599. {
  600. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  601. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  602. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  603. feedrate = homing_feedrate[X_AXIS];
  604. if(homing_feedrate[Y_AXIS]<feedrate)
  605. feedrate =homing_feedrate[Y_AXIS];
  606. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  607. st_synchronize();
  608. current_position[X_AXIS] = X_HOME_POS;
  609. current_position[Y_AXIS] = Y_HOME_POS;
  610. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  611. destination[X_AXIS] = current_position[X_AXIS];
  612. destination[Y_AXIS] = current_position[Y_AXIS];
  613. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  614. feedrate = 0.0;
  615. st_synchronize();
  616. endstops_hit_on_purpose();
  617. }
  618. #endif
  619. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  620. {
  621. HOMEAXIS(X);
  622. }
  623. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  624. HOMEAXIS(Y);
  625. }
  626. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  627. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  628. HOMEAXIS(Z);
  629. }
  630. #endif
  631. if(code_seen(axis_codes[X_AXIS]))
  632. {
  633. if(code_value_long() != 0) {
  634. current_position[X_AXIS]=code_value()+add_homeing[0];
  635. }
  636. }
  637. if(code_seen(axis_codes[Y_AXIS])) {
  638. if(code_value_long() != 0) {
  639. current_position[Y_AXIS]=code_value()+add_homeing[1];
  640. }
  641. }
  642. if(code_seen(axis_codes[Z_AXIS])) {
  643. if(code_value_long() != 0) {
  644. current_position[Z_AXIS]=code_value()+add_homeing[2];
  645. }
  646. }
  647. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  648. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  649. enable_endstops(false);
  650. #endif
  651. feedrate = saved_feedrate;
  652. feedmultiply = saved_feedmultiply;
  653. previous_millis_cmd = millis();
  654. endstops_hit_on_purpose();
  655. break;
  656. case 90: // G90
  657. relative_mode = false;
  658. break;
  659. case 91: // G91
  660. relative_mode = true;
  661. break;
  662. case 92: // G92
  663. if(!code_seen(axis_codes[E_AXIS]))
  664. st_synchronize();
  665. for(int8_t i=0; i < NUM_AXIS; i++) {
  666. if(code_seen(axis_codes[i])) {
  667. if(i == E_AXIS) {
  668. current_position[i] = code_value();
  669. plan_set_e_position(current_position[E_AXIS]);
  670. }
  671. else {
  672. current_position[i] = code_value()+add_homeing[i];
  673. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  674. }
  675. }
  676. }
  677. break;
  678. }
  679. }
  680. else if(code_seen('M'))
  681. {
  682. switch( (int)code_value() )
  683. {
  684. #ifdef ULTRA_LCD
  685. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  686. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  687. {
  688. LCD_MESSAGEPGM(MSG_USERWAIT);
  689. codenum = 0;
  690. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  691. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  692. st_synchronize();
  693. previous_millis_cmd = millis();
  694. if (codenum > 0)
  695. {
  696. codenum += millis(); // keep track of when we started waiting
  697. while(millis() < codenum && !CLICKED){
  698. manage_heater();
  699. manage_inactivity(1);
  700. LCD_STATUS;
  701. }
  702. }else{
  703. while(!CLICKED) {
  704. manage_heater();
  705. manage_inactivity(1);
  706. LCD_STATUS;
  707. }
  708. }
  709. }
  710. break;
  711. #endif
  712. case 17:
  713. LCD_MESSAGEPGM(MSG_NO_MOVE);
  714. enable_x();
  715. enable_y();
  716. enable_z();
  717. enable_e0();
  718. enable_e1();
  719. enable_e2();
  720. break;
  721. #ifdef SDSUPPORT
  722. case 20: // M20 - list SD card
  723. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  724. card.ls();
  725. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  726. break;
  727. case 21: // M21 - init SD card
  728. card.initsd();
  729. break;
  730. case 22: //M22 - release SD card
  731. card.release();
  732. break;
  733. case 23: //M23 - Select file
  734. starpos = (strchr(strchr_pointer + 4,'*'));
  735. if(starpos!=NULL)
  736. *(starpos-1)='\0';
  737. card.openFile(strchr_pointer + 4,true);
  738. break;
  739. case 24: //M24 - Start SD print
  740. card.startFileprint();
  741. starttime=millis();
  742. break;
  743. case 25: //M25 - Pause SD print
  744. card.pauseSDPrint();
  745. break;
  746. case 26: //M26 - Set SD index
  747. if(card.cardOK && code_seen('S')) {
  748. card.setIndex(code_value_long());
  749. }
  750. break;
  751. case 27: //M27 - Get SD status
  752. card.getStatus();
  753. break;
  754. case 28: //M28 - Start SD write
  755. starpos = (strchr(strchr_pointer + 4,'*'));
  756. if(starpos != NULL){
  757. char* npos = strchr(cmdbuffer[bufindr], 'N');
  758. strchr_pointer = strchr(npos,' ') + 1;
  759. *(starpos-1) = '\0';
  760. }
  761. card.openFile(strchr_pointer+4,false);
  762. break;
  763. case 29: //M29 - Stop SD write
  764. //processed in write to file routine above
  765. //card,saving = false;
  766. break;
  767. case 30: //M30 <filename> Delete File
  768. if (card.cardOK){
  769. card.closefile();
  770. starpos = (strchr(strchr_pointer + 4,'*'));
  771. if(starpos != NULL){
  772. char* npos = strchr(cmdbuffer[bufindr], 'N');
  773. strchr_pointer = strchr(npos,' ') + 1;
  774. *(starpos-1) = '\0';
  775. }
  776. card.removeFile(strchr_pointer + 4);
  777. }
  778. break;
  779. #endif //SDSUPPORT
  780. case 31: //M31 take time since the start of the SD print or an M109 command
  781. {
  782. stoptime=millis();
  783. char time[30];
  784. unsigned long t=(stoptime-starttime)/1000;
  785. int sec,min;
  786. min=t/60;
  787. sec=t%60;
  788. sprintf(time,"%i min, %i sec",min,sec);
  789. SERIAL_ECHO_START;
  790. SERIAL_ECHOLN(time);
  791. LCD_MESSAGE(time);
  792. autotempShutdown();
  793. }
  794. break;
  795. case 42: //M42 -Change pin status via gcode
  796. if (code_seen('S'))
  797. {
  798. int pin_status = code_value();
  799. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  800. {
  801. int pin_number = code_value();
  802. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  803. {
  804. if (sensitive_pins[i] == pin_number)
  805. {
  806. pin_number = -1;
  807. break;
  808. }
  809. }
  810. if (pin_number > -1)
  811. {
  812. pinMode(pin_number, OUTPUT);
  813. digitalWrite(pin_number, pin_status);
  814. analogWrite(pin_number, pin_status);
  815. }
  816. }
  817. }
  818. break;
  819. case 104: // M104
  820. tmp_extruder = active_extruder;
  821. if(code_seen('T')) {
  822. tmp_extruder = code_value();
  823. if(tmp_extruder >= EXTRUDERS) {
  824. SERIAL_ECHO_START;
  825. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  826. SERIAL_ECHOLN(tmp_extruder);
  827. break;
  828. }
  829. }
  830. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  831. setWatch();
  832. break;
  833. case 140: // M140 set bed temp
  834. if (code_seen('S')) setTargetBed(code_value());
  835. break;
  836. case 105 : // M105
  837. tmp_extruder = active_extruder;
  838. if(code_seen('T')) {
  839. tmp_extruder = code_value();
  840. if(tmp_extruder >= EXTRUDERS) {
  841. SERIAL_ECHO_START;
  842. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  843. SERIAL_ECHOLN(tmp_extruder);
  844. break;
  845. }
  846. }
  847. #if (TEMP_0_PIN > -1)
  848. SERIAL_PROTOCOLPGM("ok T:");
  849. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  850. SERIAL_PROTOCOLPGM(" /");
  851. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  852. #if TEMP_BED_PIN > -1
  853. SERIAL_PROTOCOLPGM(" B:");
  854. SERIAL_PROTOCOL_F(degBed(),1);
  855. SERIAL_PROTOCOLPGM(" /");
  856. SERIAL_PROTOCOL_F(degTargetBed(),1);
  857. #endif //TEMP_BED_PIN
  858. #else
  859. SERIAL_ERROR_START;
  860. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  861. #endif
  862. #ifdef PIDTEMP
  863. SERIAL_PROTOCOLPGM(" @:");
  864. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  865. #endif
  866. SERIAL_PROTOCOLLN("");
  867. return;
  868. break;
  869. case 109:
  870. {// M109 - Wait for extruder heater to reach target.
  871. tmp_extruder = active_extruder;
  872. if(code_seen('T')) {
  873. tmp_extruder = code_value();
  874. if(tmp_extruder >= EXTRUDERS) {
  875. SERIAL_ECHO_START;
  876. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  877. SERIAL_ECHOLN(tmp_extruder);
  878. break;
  879. }
  880. }
  881. LCD_MESSAGEPGM(MSG_HEATING);
  882. #ifdef AUTOTEMP
  883. autotemp_enabled=false;
  884. #endif
  885. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  886. #ifdef AUTOTEMP
  887. if (code_seen('S')) autotemp_min=code_value();
  888. if (code_seen('B')) autotemp_max=code_value();
  889. if (code_seen('F'))
  890. {
  891. autotemp_factor=code_value();
  892. autotemp_enabled=true;
  893. }
  894. #endif
  895. setWatch();
  896. codenum = millis();
  897. /* See if we are heating up or cooling down */
  898. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  899. #ifdef TEMP_RESIDENCY_TIME
  900. long residencyStart;
  901. residencyStart = -1;
  902. /* continue to loop until we have reached the target temp
  903. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  904. while((residencyStart == -1) ||
  905. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  906. #else
  907. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  908. #endif //TEMP_RESIDENCY_TIME
  909. if( (millis() - codenum) > 1000UL )
  910. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  911. SERIAL_PROTOCOLPGM("T:");
  912. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  913. SERIAL_PROTOCOLPGM(" E:");
  914. SERIAL_PROTOCOL((int)tmp_extruder);
  915. #ifdef TEMP_RESIDENCY_TIME
  916. SERIAL_PROTOCOLPGM(" W:");
  917. if(residencyStart > -1)
  918. {
  919. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  920. SERIAL_PROTOCOLLN( codenum );
  921. }
  922. else
  923. {
  924. SERIAL_PROTOCOLLN( "?" );
  925. }
  926. #else
  927. SERIAL_PROTOCOLLN("");
  928. #endif
  929. codenum = millis();
  930. }
  931. manage_heater();
  932. manage_inactivity(1);
  933. LCD_STATUS;
  934. #ifdef TEMP_RESIDENCY_TIME
  935. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  936. or when current temp falls outside the hysteresis after target temp was reached */
  937. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  938. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  939. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  940. {
  941. residencyStart = millis();
  942. }
  943. #endif //TEMP_RESIDENCY_TIME
  944. }
  945. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  946. starttime=millis();
  947. previous_millis_cmd = millis();
  948. }
  949. break;
  950. case 190: // M190 - Wait for bed heater to reach target.
  951. #if TEMP_BED_PIN > -1
  952. LCD_MESSAGEPGM(MSG_BED_HEATING);
  953. if (code_seen('S')) setTargetBed(code_value());
  954. codenum = millis();
  955. while(isHeatingBed())
  956. {
  957. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  958. {
  959. float tt=degHotend(active_extruder);
  960. SERIAL_PROTOCOLPGM("T:");
  961. SERIAL_PROTOCOL(tt);
  962. SERIAL_PROTOCOLPGM(" E:");
  963. SERIAL_PROTOCOL((int)active_extruder);
  964. SERIAL_PROTOCOLPGM(" B:");
  965. SERIAL_PROTOCOL_F(degBed(),1);
  966. SERIAL_PROTOCOLLN("");
  967. codenum = millis();
  968. }
  969. manage_heater();
  970. manage_inactivity(1);
  971. LCD_STATUS;
  972. }
  973. LCD_MESSAGEPGM(MSG_BED_DONE);
  974. previous_millis_cmd = millis();
  975. #endif
  976. break;
  977. #if FAN_PIN > -1
  978. case 106: //M106 Fan On
  979. if (code_seen('S')){
  980. FanSpeed=constrain(code_value(),0,255);
  981. }
  982. else {
  983. FanSpeed=255;
  984. }
  985. break;
  986. case 107: //M107 Fan Off
  987. FanSpeed = 0;
  988. break;
  989. #endif //FAN_PIN
  990. #if (PS_ON_PIN > -1)
  991. case 80: // M80 - ATX Power On
  992. SET_OUTPUT(PS_ON_PIN); //GND
  993. WRITE(PS_ON_PIN, LOW);
  994. break;
  995. #endif
  996. case 81: // M81 - ATX Power Off
  997. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  998. st_synchronize();
  999. suicide();
  1000. #elif (PS_ON_PIN > -1)
  1001. SET_INPUT(PS_ON_PIN); //Floating
  1002. #endif
  1003. break;
  1004. case 82:
  1005. axis_relative_modes[3] = false;
  1006. break;
  1007. case 83:
  1008. axis_relative_modes[3] = true;
  1009. break;
  1010. case 18: //compatibility
  1011. case 84: // M84
  1012. if(code_seen('S')){
  1013. stepper_inactive_time = code_value() * 1000;
  1014. }
  1015. else
  1016. {
  1017. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1018. if(all_axis)
  1019. {
  1020. st_synchronize();
  1021. disable_e0();
  1022. disable_e1();
  1023. disable_e2();
  1024. finishAndDisableSteppers();
  1025. }
  1026. else
  1027. {
  1028. st_synchronize();
  1029. if(code_seen('X')) disable_x();
  1030. if(code_seen('Y')) disable_y();
  1031. if(code_seen('Z')) disable_z();
  1032. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1033. if(code_seen('E')) {
  1034. disable_e0();
  1035. disable_e1();
  1036. disable_e2();
  1037. }
  1038. #endif
  1039. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1040. }
  1041. }
  1042. break;
  1043. case 85: // M85
  1044. code_seen('S');
  1045. max_inactive_time = code_value() * 1000;
  1046. break;
  1047. case 92: // M92
  1048. for(int8_t i=0; i < NUM_AXIS; i++)
  1049. {
  1050. if(code_seen(axis_codes[i]))
  1051. if(i == 3) { // E
  1052. float value = code_value();
  1053. if(value < 20.0) {
  1054. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1055. max_e_jerk *= factor;
  1056. max_feedrate[i] *= factor;
  1057. axis_steps_per_sqr_second[i] *= factor;
  1058. }
  1059. axis_steps_per_unit[i] = value;
  1060. }
  1061. else {
  1062. axis_steps_per_unit[i] = code_value();
  1063. }
  1064. }
  1065. break;
  1066. case 115: // M115
  1067. SerialprintPGM(MSG_M115_REPORT);
  1068. break;
  1069. case 117: // M117 display message
  1070. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1071. break;
  1072. case 114: // M114
  1073. SERIAL_PROTOCOLPGM("X:");
  1074. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1075. SERIAL_PROTOCOLPGM("Y:");
  1076. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1077. SERIAL_PROTOCOLPGM("Z:");
  1078. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1079. SERIAL_PROTOCOLPGM("E:");
  1080. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1081. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1082. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1083. SERIAL_PROTOCOLPGM("Y:");
  1084. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1085. SERIAL_PROTOCOLPGM("Z:");
  1086. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1087. SERIAL_PROTOCOLLN("");
  1088. break;
  1089. case 120: // M120
  1090. enable_endstops(false) ;
  1091. break;
  1092. case 121: // M121
  1093. enable_endstops(true) ;
  1094. break;
  1095. case 119: // M119
  1096. #if (X_MIN_PIN > -1)
  1097. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1098. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1099. #endif
  1100. #if (X_MAX_PIN > -1)
  1101. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1102. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1103. #endif
  1104. #if (Y_MIN_PIN > -1)
  1105. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1106. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1107. #endif
  1108. #if (Y_MAX_PIN > -1)
  1109. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1110. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1111. #endif
  1112. #if (Z_MIN_PIN > -1)
  1113. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1114. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1115. #endif
  1116. #if (Z_MAX_PIN > -1)
  1117. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1118. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1119. #endif
  1120. SERIAL_PROTOCOLLN("");
  1121. break;
  1122. //TODO: update for all axis, use for loop
  1123. case 201: // M201
  1124. for(int8_t i=0; i < NUM_AXIS; i++)
  1125. {
  1126. if(code_seen(axis_codes[i]))
  1127. {
  1128. max_acceleration_units_per_sq_second[i] = code_value();
  1129. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1130. }
  1131. }
  1132. break;
  1133. #if 0 // Not used for Sprinter/grbl gen6
  1134. case 202: // M202
  1135. for(int8_t i=0; i < NUM_AXIS; i++) {
  1136. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1137. }
  1138. break;
  1139. #endif
  1140. case 203: // M203 max feedrate mm/sec
  1141. for(int8_t i=0; i < NUM_AXIS; i++) {
  1142. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1143. }
  1144. break;
  1145. case 204: // M204 acclereration S normal moves T filmanent only moves
  1146. {
  1147. if(code_seen('S')) acceleration = code_value() ;
  1148. if(code_seen('T')) retract_acceleration = code_value() ;
  1149. }
  1150. break;
  1151. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  1152. {
  1153. if(code_seen('S')) minimumfeedrate = code_value();
  1154. if(code_seen('T')) mintravelfeedrate = code_value();
  1155. if(code_seen('B')) minsegmenttime = code_value() ;
  1156. if(code_seen('X')) max_xy_jerk = code_value() ;
  1157. if(code_seen('Z')) max_z_jerk = code_value() ;
  1158. if(code_seen('E')) max_e_jerk = code_value() ;
  1159. }
  1160. break;
  1161. case 206: // M206 additional homeing offset
  1162. for(int8_t i=0; i < 3; i++)
  1163. {
  1164. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1165. }
  1166. break;
  1167. #ifdef FWRETRACT
  1168. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1169. {
  1170. if(code_seen('S'))
  1171. {
  1172. retract_length = code_value() ;
  1173. }
  1174. if(code_seen('F'))
  1175. {
  1176. retract_feedrate = code_value() ;
  1177. }
  1178. if(code_seen('Z'))
  1179. {
  1180. retract_zlift = code_value() ;
  1181. }
  1182. }break;
  1183. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1184. {
  1185. if(code_seen('S'))
  1186. {
  1187. retract_recover_length = code_value() ;
  1188. }
  1189. if(code_seen('F'))
  1190. {
  1191. retract_recover_feedrate = code_value() ;
  1192. }
  1193. }break;
  1194. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  1195. {
  1196. if(code_seen('S'))
  1197. {
  1198. int t= code_value() ;
  1199. switch(t)
  1200. {
  1201. case 0: autoretract_enabled=false;retracted=false;break;
  1202. case 1: autoretract_enabled=true;retracted=false;break;
  1203. default:
  1204. SERIAL_ECHO_START;
  1205. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1206. SERIAL_ECHO(cmdbuffer[bufindr]);
  1207. SERIAL_ECHOLNPGM("\"");
  1208. }
  1209. }
  1210. }break;
  1211. #endif
  1212. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1213. {
  1214. if(code_seen('S'))
  1215. {
  1216. feedmultiply = code_value() ;
  1217. feedmultiplychanged=true;
  1218. }
  1219. }
  1220. break;
  1221. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1222. {
  1223. if(code_seen('S'))
  1224. {
  1225. extrudemultiply = code_value() ;
  1226. }
  1227. }
  1228. break;
  1229. #ifdef PIDTEMP
  1230. case 301: // M301
  1231. {
  1232. if(code_seen('P')) Kp = code_value();
  1233. if(code_seen('I')) Ki = code_value()*PID_dT;
  1234. if(code_seen('D')) Kd = code_value()/PID_dT;
  1235. #ifdef PID_ADD_EXTRUSION_RATE
  1236. if(code_seen('C')) Kc = code_value();
  1237. #endif
  1238. updatePID();
  1239. SERIAL_PROTOCOL(MSG_OK);
  1240. SERIAL_PROTOCOL(" p:");
  1241. SERIAL_PROTOCOL(Kp);
  1242. SERIAL_PROTOCOL(" i:");
  1243. SERIAL_PROTOCOL(Ki/PID_dT);
  1244. SERIAL_PROTOCOL(" d:");
  1245. SERIAL_PROTOCOL(Kd*PID_dT);
  1246. #ifdef PID_ADD_EXTRUSION_RATE
  1247. SERIAL_PROTOCOL(" c:");
  1248. SERIAL_PROTOCOL(Kc*PID_dT);
  1249. #endif
  1250. SERIAL_PROTOCOLLN("");
  1251. }
  1252. break;
  1253. #endif //PIDTEMP
  1254. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1255. {
  1256. #ifdef PHOTOGRAPH_PIN
  1257. #if (PHOTOGRAPH_PIN > -1)
  1258. const uint8_t NUM_PULSES=16;
  1259. const float PULSE_LENGTH=0.01524;
  1260. for(int i=0; i < NUM_PULSES; i++) {
  1261. WRITE(PHOTOGRAPH_PIN, HIGH);
  1262. _delay_ms(PULSE_LENGTH);
  1263. WRITE(PHOTOGRAPH_PIN, LOW);
  1264. _delay_ms(PULSE_LENGTH);
  1265. }
  1266. delay(7.33);
  1267. for(int i=0; i < NUM_PULSES; i++) {
  1268. WRITE(PHOTOGRAPH_PIN, HIGH);
  1269. _delay_ms(PULSE_LENGTH);
  1270. WRITE(PHOTOGRAPH_PIN, LOW);
  1271. _delay_ms(PULSE_LENGTH);
  1272. }
  1273. #endif
  1274. #endif
  1275. }
  1276. break;
  1277. case 302: // allow cold extrudes
  1278. {
  1279. allow_cold_extrudes(true);
  1280. }
  1281. break;
  1282. case 303: // M303 PID autotune
  1283. {
  1284. float temp = 150.0;
  1285. if (code_seen('S')) temp=code_value();
  1286. PID_autotune(temp);
  1287. }
  1288. break;
  1289. case 400: // M400 finish all moves
  1290. {
  1291. st_synchronize();
  1292. }
  1293. break;
  1294. case 500: // Store settings in EEPROM
  1295. {
  1296. EEPROM_StoreSettings();
  1297. }
  1298. break;
  1299. case 501: // Read settings from EEPROM
  1300. {
  1301. EEPROM_RetrieveSettings();
  1302. }
  1303. break;
  1304. case 502: // Revert to default settings
  1305. {
  1306. EEPROM_RetrieveSettings(true);
  1307. }
  1308. break;
  1309. case 503: // print settings currently in memory
  1310. {
  1311. EEPROM_printSettings();
  1312. }
  1313. break;
  1314. case 999: // Restart after being stopped
  1315. Stopped = false;
  1316. gcode_LastN = Stopped_gcode_LastN;
  1317. FlushSerialRequestResend();
  1318. break;
  1319. }
  1320. }
  1321. else if(code_seen('T'))
  1322. {
  1323. tmp_extruder = code_value();
  1324. if(tmp_extruder >= EXTRUDERS) {
  1325. SERIAL_ECHO_START;
  1326. SERIAL_ECHO("T");
  1327. SERIAL_ECHO(tmp_extruder);
  1328. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1329. }
  1330. else {
  1331. active_extruder = tmp_extruder;
  1332. SERIAL_ECHO_START;
  1333. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1334. SERIAL_PROTOCOLLN((int)active_extruder);
  1335. }
  1336. }
  1337. else
  1338. {
  1339. SERIAL_ECHO_START;
  1340. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1341. SERIAL_ECHO(cmdbuffer[bufindr]);
  1342. SERIAL_ECHOLNPGM("\"");
  1343. }
  1344. ClearToSend();
  1345. }
  1346. void FlushSerialRequestResend()
  1347. {
  1348. //char cmdbuffer[bufindr][100]="Resend:";
  1349. MYSERIAL.flush();
  1350. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1351. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1352. ClearToSend();
  1353. }
  1354. void ClearToSend()
  1355. {
  1356. previous_millis_cmd = millis();
  1357. #ifdef SDSUPPORT
  1358. if(fromsd[bufindr])
  1359. return;
  1360. #endif //SDSUPPORT
  1361. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1362. }
  1363. void get_coordinates()
  1364. {
  1365. bool seen[4]={false,false,false,false};
  1366. for(int8_t i=0; i < NUM_AXIS; i++) {
  1367. if(code_seen(axis_codes[i]))
  1368. {
  1369. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1370. seen[i]=true;
  1371. }
  1372. else destination[i] = current_position[i]; //Are these else lines really needed?
  1373. }
  1374. if(code_seen('F')) {
  1375. next_feedrate = code_value();
  1376. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1377. }
  1378. #ifdef FWRETRACT
  1379. if(autoretract_enabled)
  1380. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1381. {
  1382. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1383. if(echange<-MIN_RETRACT) //retract
  1384. {
  1385. if(!retracted)
  1386. {
  1387. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1388. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1389. float correctede=-echange-retract_length;
  1390. //to generate the additional steps, not the destination is changed, but inversely the current position
  1391. current_position[E_AXIS]+=-correctede;
  1392. feedrate=retract_feedrate;
  1393. retracted=true;
  1394. }
  1395. }
  1396. else
  1397. if(echange>MIN_RETRACT) //retract_recover
  1398. {
  1399. if(retracted)
  1400. {
  1401. //current_position[Z_AXIS]+=-retract_zlift;
  1402. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1403. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1404. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1405. feedrate=retract_recover_feedrate;
  1406. retracted=false;
  1407. }
  1408. }
  1409. }
  1410. #endif //FWRETRACT
  1411. }
  1412. void get_arc_coordinates()
  1413. {
  1414. get_coordinates();
  1415. if(code_seen('I')) {
  1416. offset[0] = code_value();
  1417. }
  1418. else {
  1419. offset[0] = 0.0;
  1420. }
  1421. if(code_seen('J')) {
  1422. offset[1] = code_value();
  1423. }
  1424. else {
  1425. offset[1] = 0.0;
  1426. }
  1427. }
  1428. void prepare_move()
  1429. {
  1430. if (min_software_endstops) {
  1431. if (destination[X_AXIS] < X_MIN_POS) destination[X_AXIS] = X_MIN_POS;
  1432. if (destination[Y_AXIS] < Y_MIN_POS) destination[Y_AXIS] = Y_MIN_POS;
  1433. if (destination[Z_AXIS] < Z_MIN_POS) destination[Z_AXIS] = Z_MIN_POS;
  1434. }
  1435. if (max_software_endstops) {
  1436. if (destination[X_AXIS] > X_MAX_POS) destination[X_AXIS] = X_MAX_POS;
  1437. if (destination[Y_AXIS] > Y_MAX_POS) destination[Y_AXIS] = Y_MAX_POS;
  1438. if (destination[Z_AXIS] > Z_MAX_POS) destination[Z_AXIS] = Z_MAX_POS;
  1439. }
  1440. previous_millis_cmd = millis();
  1441. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1442. for(int8_t i=0; i < NUM_AXIS; i++) {
  1443. current_position[i] = destination[i];
  1444. }
  1445. }
  1446. void prepare_arc_move(char isclockwise) {
  1447. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1448. // Trace the arc
  1449. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1450. // As far as the parser is concerned, the position is now == target. In reality the
  1451. // motion control system might still be processing the action and the real tool position
  1452. // in any intermediate location.
  1453. for(int8_t i=0; i < NUM_AXIS; i++) {
  1454. current_position[i] = destination[i];
  1455. }
  1456. previous_millis_cmd = millis();
  1457. }
  1458. #ifdef CONTROLLERFAN_PIN
  1459. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1460. unsigned long lastMotorCheck = 0;
  1461. void controllerFan()
  1462. {
  1463. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1464. {
  1465. lastMotorCheck = millis();
  1466. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1467. #if EXTRUDERS > 2
  1468. || !READ(E2_ENABLE_PIN)
  1469. #endif
  1470. #if EXTRUDER > 1
  1471. || !READ(E2_ENABLE_PIN)
  1472. #endif
  1473. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1474. {
  1475. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1476. }
  1477. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1478. {
  1479. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1480. }
  1481. else
  1482. {
  1483. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1484. }
  1485. }
  1486. }
  1487. #endif
  1488. void manage_inactivity(byte debug)
  1489. {
  1490. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1491. if(max_inactive_time)
  1492. kill();
  1493. if(stepper_inactive_time) {
  1494. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1495. {
  1496. if(blocks_queued() == false) {
  1497. disable_x();
  1498. disable_y();
  1499. disable_z();
  1500. disable_e0();
  1501. disable_e1();
  1502. disable_e2();
  1503. }
  1504. }
  1505. }
  1506. #ifdef CONTROLLERFAN_PIN
  1507. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1508. #endif
  1509. #ifdef EXTRUDER_RUNOUT_PREVENT
  1510. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1511. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1512. {
  1513. bool oldstatus=READ(E0_ENABLE_PIN);
  1514. enable_e0();
  1515. float oldepos=current_position[E_AXIS];
  1516. float oldedes=destination[E_AXIS];
  1517. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1518. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1519. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1520. current_position[E_AXIS]=oldepos;
  1521. destination[E_AXIS]=oldedes;
  1522. plan_set_e_position(oldepos);
  1523. previous_millis_cmd=millis();
  1524. st_synchronize();
  1525. WRITE(E0_ENABLE_PIN,oldstatus);
  1526. }
  1527. #endif
  1528. check_axes_activity();
  1529. }
  1530. void kill()
  1531. {
  1532. cli(); // Stop interrupts
  1533. disable_heater();
  1534. disable_x();
  1535. disable_y();
  1536. disable_z();
  1537. disable_e0();
  1538. disable_e1();
  1539. disable_e2();
  1540. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1541. SERIAL_ERROR_START;
  1542. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1543. LCD_MESSAGEPGM(MSG_KILLED);
  1544. suicide();
  1545. while(1); // Wait for reset
  1546. }
  1547. void Stop()
  1548. {
  1549. disable_heater();
  1550. if(Stopped == false) {
  1551. Stopped = true;
  1552. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1553. SERIAL_ERROR_START;
  1554. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1555. LCD_MESSAGEPGM(MSG_STOPPED);
  1556. }
  1557. }
  1558. bool IsStopped() { return Stopped; };
  1559. #ifdef FAST_PWM_FAN
  1560. void setPwmFrequency(uint8_t pin, int val)
  1561. {
  1562. val &= 0x07;
  1563. switch(digitalPinToTimer(pin))
  1564. {
  1565. #if defined(TCCR0A)
  1566. case TIMER0A:
  1567. case TIMER0B:
  1568. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1569. // TCCR0B |= val;
  1570. break;
  1571. #endif
  1572. #if defined(TCCR1A)
  1573. case TIMER1A:
  1574. case TIMER1B:
  1575. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1576. // TCCR1B |= val;
  1577. break;
  1578. #endif
  1579. #if defined(TCCR2)
  1580. case TIMER2:
  1581. case TIMER2:
  1582. TCCR2 &= ~(CS10 | CS11 | CS12);
  1583. TCCR2 |= val;
  1584. break;
  1585. #endif
  1586. #if defined(TCCR2A)
  1587. case TIMER2A:
  1588. case TIMER2B:
  1589. TCCR2B &= ~(CS20 | CS21 | CS22);
  1590. TCCR2B |= val;
  1591. break;
  1592. #endif
  1593. #if defined(TCCR3A)
  1594. case TIMER3A:
  1595. case TIMER3B:
  1596. case TIMER3C:
  1597. TCCR3B &= ~(CS30 | CS31 | CS32);
  1598. TCCR3B |= val;
  1599. break;
  1600. #endif
  1601. #if defined(TCCR4A)
  1602. case TIMER4A:
  1603. case TIMER4B:
  1604. case TIMER4C:
  1605. TCCR4B &= ~(CS40 | CS41 | CS42);
  1606. TCCR4B |= val;
  1607. break;
  1608. #endif
  1609. #if defined(TCCR5A)
  1610. case TIMER5A:
  1611. case TIMER5B:
  1612. case TIMER5C:
  1613. TCCR5B &= ~(CS50 | CS51 | CS52);
  1614. TCCR5B |= val;
  1615. break;
  1616. #endif
  1617. }
  1618. }
  1619. #endif