My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.pde 53KB

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