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_main.cpp 69KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  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 "ConfigurationStore.h"
  32. #include "language.h"
  33. #include "pins_arduino.h"
  34. #if NUM_SERVOS > 0
  35. #include "Servo.h"
  36. #endif
  37. #if DIGIPOTSS_PIN > 0
  38. #include <SPI.h>
  39. #endif
  40. #define VERSION_STRING "1.0.0"
  41. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  42. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  43. //Implemented Codes
  44. //-------------------
  45. // G0 -> G1
  46. // G1 - Coordinated Movement X Y Z E
  47. // G2 - CW ARC
  48. // G3 - CCW ARC
  49. // G4 - Dwell S<seconds> or P<milliseconds>
  50. // G10 - retract filament according to settings of M207
  51. // G11 - retract recover filament according to settings of M208
  52. // G28 - Home all Axis
  53. // G90 - Use Absolute Coordinates
  54. // G91 - Use Relative Coordinates
  55. // G92 - Set current position to cordinates given
  56. //RepRap M Codes
  57. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  58. // M1 - Same as M0
  59. // M104 - Set extruder target temp
  60. // M105 - Read current temp
  61. // M106 - Fan on
  62. // M107 - Fan off
  63. // M109 - Wait for extruder current temp to reach target temp.
  64. // M114 - Display current position
  65. //Custom M Codes
  66. // M17 - Enable/Power all stepper motors
  67. // M18 - Disable all stepper motors; same as M84
  68. // M20 - List SD card
  69. // M21 - Init SD card
  70. // M22 - Release SD card
  71. // M23 - Select SD file (M23 filename.g)
  72. // M24 - Start/resume SD print
  73. // M25 - Pause SD print
  74. // M26 - Set SD position in bytes (M26 S12345)
  75. // M27 - Report SD print status
  76. // M28 - Start SD write (M28 filename.g)
  77. // M29 - Stop SD write
  78. // M30 - Delete file from SD (M30 filename.g)
  79. // M31 - Output time since last M109 or SD card start to serial
  80. // M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used.
  81. // M80 - Turn on Power Supply
  82. // M81 - Turn off Power Supply
  83. // M82 - Set E codes absolute (default)
  84. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  85. // M84 - Disable steppers until next move,
  86. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  87. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  88. // M92 - Set axis_steps_per_unit - same syntax as G92
  89. // M114 - Output current position to serial port
  90. // M115 - Capabilities string
  91. // M117 - display message
  92. // M119 - Output Endstop status to serial port
  93. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  94. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  95. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  96. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  97. // M140 - Set bed target temp
  98. // M190 - Wait for bed current temp to reach target temp.
  99. // M200 - Set filament diameter
  100. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  101. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  102. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  103. // 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
  104. // 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
  105. // M206 - set additional homeing offset
  106. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  107. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  108. // 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.
  109. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  110. // M220 S<factor in percent>- set speed factor override percentage
  111. // M221 S<factor in percent>- set extrude factor override percentage
  112. // M240 - Trigger a camera to take a photograph
  113. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  114. // M300 - Play beepsound S<frequency Hz> P<duration ms>
  115. // M301 - Set PID parameters P I and D
  116. // M302 - Allow cold extrudes
  117. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  118. // M304 - Set bed PID parameters P I and D
  119. // M400 - Finish all moves
  120. // M500 - stores paramters in EEPROM
  121. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  122. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  123. // M503 - print the current settings (from memory not from eeprom)
  124. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  125. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  126. // M907 - Set digital trimpot motor current using axis codes.
  127. // M908 - Control digital trimpot directly.
  128. // M350 - Set microstepping mode.
  129. // M351 - Toggle MS1 MS2 pins directly.
  130. // M928 - Start SD logging (M928 filename.g) - ended by M29
  131. // M999 - Restart after being stopped by error
  132. //Stepper Movement Variables
  133. //===========================================================================
  134. //=============================imported variables============================
  135. //===========================================================================
  136. //===========================================================================
  137. //=============================public variables=============================
  138. //===========================================================================
  139. #ifdef SDSUPPORT
  140. CardReader card;
  141. #endif
  142. float homing_feedrate[] = HOMING_FEEDRATE;
  143. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  144. int feedmultiply=100; //100->1 200->2
  145. int saved_feedmultiply;
  146. int extrudemultiply=100; //100->1 200->2
  147. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  148. float add_homeing[3]={0,0,0};
  149. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  150. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  151. // Extruder offset, only in XY plane
  152. #if EXTRUDERS > 1
  153. float extruder_offset[2][EXTRUDERS] = {
  154. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  155. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  156. #endif
  157. };
  158. #endif
  159. uint8_t active_extruder = 0;
  160. int fanSpeed=0;
  161. #ifdef BARICUDA
  162. int ValvePressure=0;
  163. int EtoPPressure=0;
  164. #endif
  165. #ifdef FWRETRACT
  166. bool autoretract_enabled=true;
  167. bool retracted=false;
  168. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  169. float retract_recover_length=0, retract_recover_feedrate=8*60;
  170. #endif
  171. //===========================================================================
  172. //=============================private variables=============================
  173. //===========================================================================
  174. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  175. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  176. static float offset[3] = {0.0, 0.0, 0.0};
  177. static bool home_all_axis = true;
  178. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  179. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  180. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  181. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  182. static bool fromsd[BUFSIZE];
  183. static int bufindr = 0;
  184. static int bufindw = 0;
  185. static int buflen = 0;
  186. //static int i = 0;
  187. static char serial_char;
  188. static int serial_count = 0;
  189. static boolean comment_mode = false;
  190. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  191. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  192. //static float tt = 0;
  193. //static float bt = 0;
  194. //Inactivity shutdown variables
  195. static unsigned long previous_millis_cmd = 0;
  196. static unsigned long max_inactive_time = 0;
  197. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  198. unsigned long starttime=0;
  199. unsigned long stoptime=0;
  200. static uint8_t tmp_extruder;
  201. bool Stopped=false;
  202. #if NUM_SERVOS > 0
  203. Servo servos[NUM_SERVOS];
  204. #endif
  205. //===========================================================================
  206. //=============================ROUTINES=============================
  207. //===========================================================================
  208. void get_arc_coordinates();
  209. bool setTargetedHotend(int code);
  210. void serial_echopair_P(const char *s_P, float v)
  211. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  212. void serial_echopair_P(const char *s_P, double v)
  213. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  214. void serial_echopair_P(const char *s_P, unsigned long v)
  215. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  216. extern "C"{
  217. extern unsigned int __bss_end;
  218. extern unsigned int __heap_start;
  219. extern void *__brkval;
  220. int freeMemory() {
  221. int free_memory;
  222. if((int)__brkval == 0)
  223. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  224. else
  225. free_memory = ((int)&free_memory) - ((int)__brkval);
  226. return free_memory;
  227. }
  228. }
  229. //adds an command to the main command buffer
  230. //thats really done in a non-safe way.
  231. //needs overworking someday
  232. void enquecommand(const char *cmd)
  233. {
  234. if(buflen < BUFSIZE)
  235. {
  236. //this is dangerous if a mixing of serial and this happsens
  237. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  238. SERIAL_ECHO_START;
  239. SERIAL_ECHOPGM("enqueing \"");
  240. SERIAL_ECHO(cmdbuffer[bufindw]);
  241. SERIAL_ECHOLNPGM("\"");
  242. bufindw= (bufindw + 1)%BUFSIZE;
  243. buflen += 1;
  244. }
  245. }
  246. void enquecommand_P(const char *cmd)
  247. {
  248. if(buflen < BUFSIZE)
  249. {
  250. //this is dangerous if a mixing of serial and this happsens
  251. strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
  252. SERIAL_ECHO_START;
  253. SERIAL_ECHOPGM("enqueing \"");
  254. SERIAL_ECHO(cmdbuffer[bufindw]);
  255. SERIAL_ECHOLNPGM("\"");
  256. bufindw= (bufindw + 1)%BUFSIZE;
  257. buflen += 1;
  258. }
  259. }
  260. void setup_killpin()
  261. {
  262. #if( KILL_PIN>-1 )
  263. pinMode(KILL_PIN,INPUT);
  264. WRITE(KILL_PIN,HIGH);
  265. #endif
  266. }
  267. void setup_photpin()
  268. {
  269. #ifdef PHOTOGRAPH_PIN
  270. #if (PHOTOGRAPH_PIN > 0)
  271. SET_OUTPUT(PHOTOGRAPH_PIN);
  272. WRITE(PHOTOGRAPH_PIN, LOW);
  273. #endif
  274. #endif
  275. }
  276. void setup_powerhold()
  277. {
  278. #ifdef SUICIDE_PIN
  279. #if (SUICIDE_PIN> 0)
  280. SET_OUTPUT(SUICIDE_PIN);
  281. WRITE(SUICIDE_PIN, HIGH);
  282. #endif
  283. #endif
  284. #if (PS_ON_PIN > 0)
  285. SET_OUTPUT(PS_ON_PIN);
  286. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  287. #endif
  288. }
  289. void suicide()
  290. {
  291. #ifdef SUICIDE_PIN
  292. #if (SUICIDE_PIN > 0)
  293. SET_OUTPUT(SUICIDE_PIN);
  294. WRITE(SUICIDE_PIN, LOW);
  295. #endif
  296. #endif
  297. }
  298. void servo_init()
  299. {
  300. #if (NUM_SERVOS >= 1) && (SERVO0_PIN > 0)
  301. servos[0].attach(SERVO0_PIN);
  302. #endif
  303. #if (NUM_SERVOS >= 2) && (SERVO1_PIN > 0)
  304. servos[1].attach(SERVO1_PIN);
  305. #endif
  306. #if (NUM_SERVOS >= 3) && (SERVO2_PIN > 0)
  307. servos[2].attach(SERVO2_PIN);
  308. #endif
  309. #if (NUM_SERVOS >= 4) && (SERVO3_PIN > 0)
  310. servos[3].attach(SERVO3_PIN);
  311. #endif
  312. #if (NUM_SERVOS >= 5)
  313. #error "TODO: enter initalisation code for more servos"
  314. #endif
  315. }
  316. void setup()
  317. {
  318. setup_killpin();
  319. setup_powerhold();
  320. MYSERIAL.begin(BAUDRATE);
  321. SERIAL_PROTOCOLLNPGM("start");
  322. SERIAL_ECHO_START;
  323. // Check startup - does nothing if bootloader sets MCUSR to 0
  324. byte mcu = MCUSR;
  325. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  326. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  327. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  328. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  329. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  330. MCUSR=0;
  331. SERIAL_ECHOPGM(MSG_MARLIN);
  332. SERIAL_ECHOLNPGM(VERSION_STRING);
  333. #ifdef STRING_VERSION_CONFIG_H
  334. #ifdef STRING_CONFIG_H_AUTHOR
  335. SERIAL_ECHO_START;
  336. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  337. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  338. SERIAL_ECHOPGM(MSG_AUTHOR);
  339. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  340. SERIAL_ECHOPGM("Compiled: ");
  341. SERIAL_ECHOLNPGM(__DATE__);
  342. #endif
  343. #endif
  344. SERIAL_ECHO_START;
  345. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  346. SERIAL_ECHO(freeMemory());
  347. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  348. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  349. for(int8_t i = 0; i < BUFSIZE; i++)
  350. {
  351. fromsd[i] = false;
  352. }
  353. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  354. Config_RetrieveSettings();
  355. tp_init(); // Initialize temperature loop
  356. plan_init(); // Initialize planner;
  357. watchdog_init();
  358. st_init(); // Initialize stepper, this enables interrupts!
  359. setup_photpin();
  360. servo_init();
  361. lcd_init();
  362. #if CONTROLLERFAN_PIN > 0
  363. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  364. #endif
  365. }
  366. void loop()
  367. {
  368. if(buflen < (BUFSIZE-1))
  369. get_command();
  370. #ifdef SDSUPPORT
  371. card.checkautostart(false);
  372. #endif
  373. if(buflen)
  374. {
  375. #ifdef SDSUPPORT
  376. if(card.saving)
  377. {
  378. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  379. {
  380. card.write_command(cmdbuffer[bufindr]);
  381. if(card.logging)
  382. {
  383. process_commands();
  384. }
  385. else
  386. {
  387. SERIAL_PROTOCOLLNPGM(MSG_OK);
  388. }
  389. }
  390. else
  391. {
  392. card.closefile();
  393. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  394. }
  395. }
  396. else
  397. {
  398. process_commands();
  399. }
  400. #else
  401. process_commands();
  402. #endif //SDSUPPORT
  403. buflen = (buflen-1);
  404. bufindr = (bufindr + 1)%BUFSIZE;
  405. }
  406. //check heater every n milliseconds
  407. manage_heater();
  408. manage_inactivity();
  409. checkHitEndstops();
  410. lcd_update();
  411. }
  412. void get_command()
  413. {
  414. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  415. serial_char = MYSERIAL.read();
  416. if(serial_char == '\n' ||
  417. serial_char == '\r' ||
  418. (serial_char == ':' && comment_mode == false) ||
  419. serial_count >= (MAX_CMD_SIZE - 1) )
  420. {
  421. if(!serial_count) { //if empty line
  422. comment_mode = false; //for new command
  423. return;
  424. }
  425. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  426. if(!comment_mode){
  427. comment_mode = false; //for new command
  428. fromsd[bufindw] = false;
  429. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  430. {
  431. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  432. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  433. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  434. SERIAL_ERROR_START;
  435. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  436. SERIAL_ERRORLN(gcode_LastN);
  437. //Serial.println(gcode_N);
  438. FlushSerialRequestResend();
  439. serial_count = 0;
  440. return;
  441. }
  442. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  443. {
  444. byte checksum = 0;
  445. byte count = 0;
  446. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  447. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  448. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  449. SERIAL_ERROR_START;
  450. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  451. SERIAL_ERRORLN(gcode_LastN);
  452. FlushSerialRequestResend();
  453. serial_count = 0;
  454. return;
  455. }
  456. //if no errors, continue parsing
  457. }
  458. else
  459. {
  460. SERIAL_ERROR_START;
  461. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  462. SERIAL_ERRORLN(gcode_LastN);
  463. FlushSerialRequestResend();
  464. serial_count = 0;
  465. return;
  466. }
  467. gcode_LastN = gcode_N;
  468. //if no errors, continue parsing
  469. }
  470. else // if we don't receive 'N' but still see '*'
  471. {
  472. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  473. {
  474. SERIAL_ERROR_START;
  475. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  476. SERIAL_ERRORLN(gcode_LastN);
  477. serial_count = 0;
  478. return;
  479. }
  480. }
  481. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  482. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  483. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  484. case 0:
  485. case 1:
  486. case 2:
  487. case 3:
  488. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  489. #ifdef SDSUPPORT
  490. if(card.saving)
  491. break;
  492. #endif //SDSUPPORT
  493. SERIAL_PROTOCOLLNPGM(MSG_OK);
  494. }
  495. else {
  496. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  497. LCD_MESSAGEPGM(MSG_STOPPED);
  498. }
  499. break;
  500. default:
  501. break;
  502. }
  503. }
  504. bufindw = (bufindw + 1)%BUFSIZE;
  505. buflen += 1;
  506. }
  507. serial_count = 0; //clear buffer
  508. }
  509. else
  510. {
  511. if(serial_char == ';') comment_mode = true;
  512. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  513. }
  514. }
  515. #ifdef SDSUPPORT
  516. if(!card.sdprinting || serial_count!=0){
  517. return;
  518. }
  519. while( !card.eof() && buflen < BUFSIZE) {
  520. int16_t n=card.get();
  521. serial_char = (char)n;
  522. if(serial_char == '\n' ||
  523. serial_char == '\r' ||
  524. (serial_char == ':' && comment_mode == false) ||
  525. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  526. {
  527. if(card.eof()){
  528. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  529. stoptime=millis();
  530. char time[30];
  531. unsigned long t=(stoptime-starttime)/1000;
  532. int hours, minutes;
  533. minutes=(t/60)%60;
  534. hours=t/60/60;
  535. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  536. SERIAL_ECHO_START;
  537. SERIAL_ECHOLN(time);
  538. lcd_setstatus(time);
  539. card.printingHasFinished();
  540. card.checkautostart(true);
  541. }
  542. if(!serial_count)
  543. {
  544. comment_mode = false; //for new command
  545. return; //if empty line
  546. }
  547. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  548. // if(!comment_mode){
  549. fromsd[bufindw] = true;
  550. buflen += 1;
  551. bufindw = (bufindw + 1)%BUFSIZE;
  552. // }
  553. comment_mode = false; //for new command
  554. serial_count = 0; //clear buffer
  555. }
  556. else
  557. {
  558. if(serial_char == ';') comment_mode = true;
  559. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  560. }
  561. }
  562. #endif //SDSUPPORT
  563. }
  564. float code_value()
  565. {
  566. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  567. }
  568. long code_value_long()
  569. {
  570. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  571. }
  572. bool code_seen(char code)
  573. {
  574. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  575. return (strchr_pointer != NULL); //Return True if a character was found
  576. }
  577. #define DEFINE_PGM_READ_ANY(type, reader) \
  578. static inline type pgm_read_any(const type *p) \
  579. { return pgm_read_##reader##_near(p); }
  580. DEFINE_PGM_READ_ANY(float, float);
  581. DEFINE_PGM_READ_ANY(signed char, byte);
  582. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  583. static const PROGMEM type array##_P[3] = \
  584. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  585. static inline type array(int axis) \
  586. { return pgm_read_any(&array##_P[axis]); }
  587. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  588. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  589. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  590. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  591. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  592. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  593. static void axis_is_at_home(int axis) {
  594. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  595. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  596. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  597. }
  598. static void homeaxis(int axis) {
  599. #define HOMEAXIS_DO(LETTER) \
  600. ((LETTER##_MIN_PIN > 0 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > 0 && LETTER##_HOME_DIR==1))
  601. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  602. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  603. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  604. 0) {
  605. current_position[axis] = 0;
  606. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  607. destination[axis] = 1.5 * max_length(axis) * home_dir(axis);
  608. feedrate = homing_feedrate[axis];
  609. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  610. st_synchronize();
  611. current_position[axis] = 0;
  612. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  613. destination[axis] = -home_retract_mm(axis) * home_dir(axis);
  614. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  615. st_synchronize();
  616. destination[axis] = 2*home_retract_mm(axis) * home_dir(axis);
  617. feedrate = homing_feedrate[axis]/2 ;
  618. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  619. st_synchronize();
  620. axis_is_at_home(axis);
  621. destination[axis] = current_position[axis];
  622. feedrate = 0.0;
  623. endstops_hit_on_purpose();
  624. }
  625. }
  626. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  627. void process_commands()
  628. {
  629. unsigned long codenum; //throw away variable
  630. char *starpos = NULL;
  631. if(code_seen('G'))
  632. {
  633. switch((int)code_value())
  634. {
  635. case 0: // G0 -> G1
  636. case 1: // G1
  637. if(Stopped == false) {
  638. get_coordinates(); // For X Y Z E F
  639. prepare_move();
  640. //ClearToSend();
  641. return;
  642. }
  643. //break;
  644. case 2: // G2 - CW ARC
  645. if(Stopped == false) {
  646. get_arc_coordinates();
  647. prepare_arc_move(true);
  648. return;
  649. }
  650. case 3: // G3 - CCW ARC
  651. if(Stopped == false) {
  652. get_arc_coordinates();
  653. prepare_arc_move(false);
  654. return;
  655. }
  656. case 4: // G4 dwell
  657. LCD_MESSAGEPGM(MSG_DWELL);
  658. codenum = 0;
  659. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  660. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  661. st_synchronize();
  662. codenum += millis(); // keep track of when we started waiting
  663. previous_millis_cmd = millis();
  664. while(millis() < codenum ){
  665. manage_heater();
  666. manage_inactivity();
  667. lcd_update();
  668. }
  669. break;
  670. #ifdef FWRETRACT
  671. case 10: // G10 retract
  672. if(!retracted)
  673. {
  674. destination[X_AXIS]=current_position[X_AXIS];
  675. destination[Y_AXIS]=current_position[Y_AXIS];
  676. destination[Z_AXIS]=current_position[Z_AXIS];
  677. current_position[Z_AXIS]+=-retract_zlift;
  678. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  679. feedrate=retract_feedrate;
  680. retracted=true;
  681. prepare_move();
  682. }
  683. break;
  684. case 11: // G10 retract_recover
  685. if(!retracted)
  686. {
  687. destination[X_AXIS]=current_position[X_AXIS];
  688. destination[Y_AXIS]=current_position[Y_AXIS];
  689. destination[Z_AXIS]=current_position[Z_AXIS];
  690. current_position[Z_AXIS]+=retract_zlift;
  691. current_position[E_AXIS]+=-retract_recover_length;
  692. feedrate=retract_recover_feedrate;
  693. retracted=false;
  694. prepare_move();
  695. }
  696. break;
  697. #endif //FWRETRACT
  698. case 28: //G28 Home all Axis one at a time
  699. saved_feedrate = feedrate;
  700. saved_feedmultiply = feedmultiply;
  701. feedmultiply = 100;
  702. previous_millis_cmd = millis();
  703. enable_endstops(true);
  704. for(int8_t i=0; i < NUM_AXIS; i++) {
  705. destination[i] = current_position[i];
  706. }
  707. feedrate = 0.0;
  708. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  709. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  710. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  711. HOMEAXIS(Z);
  712. }
  713. #endif
  714. #ifdef QUICK_HOME
  715. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  716. {
  717. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  718. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  719. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  720. feedrate = homing_feedrate[X_AXIS];
  721. if(homing_feedrate[Y_AXIS]<feedrate)
  722. feedrate =homing_feedrate[Y_AXIS];
  723. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  724. st_synchronize();
  725. axis_is_at_home(X_AXIS);
  726. axis_is_at_home(Y_AXIS);
  727. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  728. destination[X_AXIS] = current_position[X_AXIS];
  729. destination[Y_AXIS] = current_position[Y_AXIS];
  730. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  731. feedrate = 0.0;
  732. st_synchronize();
  733. endstops_hit_on_purpose();
  734. }
  735. #endif
  736. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  737. {
  738. HOMEAXIS(X);
  739. }
  740. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  741. HOMEAXIS(Y);
  742. }
  743. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  744. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  745. HOMEAXIS(Z);
  746. }
  747. #endif
  748. if(code_seen(axis_codes[X_AXIS]))
  749. {
  750. if(code_value_long() != 0) {
  751. current_position[X_AXIS]=code_value()+add_homeing[0];
  752. }
  753. }
  754. if(code_seen(axis_codes[Y_AXIS])) {
  755. if(code_value_long() != 0) {
  756. current_position[Y_AXIS]=code_value()+add_homeing[1];
  757. }
  758. }
  759. if(code_seen(axis_codes[Z_AXIS])) {
  760. if(code_value_long() != 0) {
  761. current_position[Z_AXIS]=code_value()+add_homeing[2];
  762. }
  763. }
  764. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  765. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  766. enable_endstops(false);
  767. #endif
  768. feedrate = saved_feedrate;
  769. feedmultiply = saved_feedmultiply;
  770. previous_millis_cmd = millis();
  771. endstops_hit_on_purpose();
  772. break;
  773. case 90: // G90
  774. relative_mode = false;
  775. break;
  776. case 91: // G91
  777. relative_mode = true;
  778. break;
  779. case 92: // G92
  780. if(!code_seen(axis_codes[E_AXIS]))
  781. st_synchronize();
  782. for(int8_t i=0; i < NUM_AXIS; i++) {
  783. if(code_seen(axis_codes[i])) {
  784. if(i == E_AXIS) {
  785. current_position[i] = code_value();
  786. plan_set_e_position(current_position[E_AXIS]);
  787. }
  788. else {
  789. current_position[i] = code_value()+add_homeing[i];
  790. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  791. }
  792. }
  793. }
  794. break;
  795. }
  796. }
  797. else if(code_seen('M'))
  798. {
  799. switch( (int)code_value() )
  800. {
  801. #ifdef ULTIPANEL
  802. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  803. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  804. {
  805. LCD_MESSAGEPGM(MSG_USERWAIT);
  806. codenum = 0;
  807. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  808. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  809. st_synchronize();
  810. previous_millis_cmd = millis();
  811. if (codenum > 0){
  812. codenum += millis(); // keep track of when we started waiting
  813. while(millis() < codenum && !lcd_clicked()){
  814. manage_heater();
  815. manage_inactivity();
  816. lcd_update();
  817. }
  818. }else{
  819. while(!lcd_clicked()){
  820. manage_heater();
  821. manage_inactivity();
  822. lcd_update();
  823. }
  824. }
  825. LCD_MESSAGEPGM(MSG_RESUMING);
  826. }
  827. break;
  828. #endif
  829. case 17:
  830. LCD_MESSAGEPGM(MSG_NO_MOVE);
  831. enable_x();
  832. enable_y();
  833. enable_z();
  834. enable_e0();
  835. enable_e1();
  836. enable_e2();
  837. break;
  838. #ifdef SDSUPPORT
  839. case 20: // M20 - list SD card
  840. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  841. card.ls();
  842. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  843. break;
  844. case 21: // M21 - init SD card
  845. card.initsd();
  846. break;
  847. case 22: //M22 - release SD card
  848. card.release();
  849. break;
  850. case 23: //M23 - Select file
  851. starpos = (strchr(strchr_pointer + 4,'*'));
  852. if(starpos!=NULL)
  853. *(starpos-1)='\0';
  854. card.openFile(strchr_pointer + 4,true);
  855. break;
  856. case 24: //M24 - Start SD print
  857. card.startFileprint();
  858. starttime=millis();
  859. break;
  860. case 25: //M25 - Pause SD print
  861. card.pauseSDPrint();
  862. break;
  863. case 26: //M26 - Set SD index
  864. if(card.cardOK && code_seen('S')) {
  865. card.setIndex(code_value_long());
  866. }
  867. break;
  868. case 27: //M27 - Get SD status
  869. card.getStatus();
  870. break;
  871. case 28: //M28 - Start SD write
  872. starpos = (strchr(strchr_pointer + 4,'*'));
  873. if(starpos != NULL){
  874. char* npos = strchr(cmdbuffer[bufindr], 'N');
  875. strchr_pointer = strchr(npos,' ') + 1;
  876. *(starpos-1) = '\0';
  877. }
  878. card.openFile(strchr_pointer+4,false);
  879. break;
  880. case 29: //M29 - Stop SD write
  881. //processed in write to file routine above
  882. //card,saving = false;
  883. break;
  884. case 30: //M30 <filename> Delete File
  885. if (card.cardOK){
  886. card.closefile();
  887. starpos = (strchr(strchr_pointer + 4,'*'));
  888. if(starpos != NULL){
  889. char* npos = strchr(cmdbuffer[bufindr], 'N');
  890. strchr_pointer = strchr(npos,' ') + 1;
  891. *(starpos-1) = '\0';
  892. }
  893. card.removeFile(strchr_pointer + 4);
  894. }
  895. break;
  896. case 928: //M928 - Start SD write
  897. starpos = (strchr(strchr_pointer + 5,'*'));
  898. if(starpos != NULL){
  899. char* npos = strchr(cmdbuffer[bufindr], 'N');
  900. strchr_pointer = strchr(npos,' ') + 1;
  901. *(starpos-1) = '\0';
  902. }
  903. card.openLogFile(strchr_pointer+5);
  904. break;
  905. #endif //SDSUPPORT
  906. case 31: //M31 take time since the start of the SD print or an M109 command
  907. {
  908. stoptime=millis();
  909. char time[30];
  910. unsigned long t=(stoptime-starttime)/1000;
  911. int sec,min;
  912. min=t/60;
  913. sec=t%60;
  914. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  915. SERIAL_ECHO_START;
  916. SERIAL_ECHOLN(time);
  917. lcd_setstatus(time);
  918. autotempShutdown();
  919. }
  920. break;
  921. case 42: //M42 -Change pin status via gcode
  922. if (code_seen('S'))
  923. {
  924. int pin_status = code_value();
  925. int pin_number = LED_PIN;
  926. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  927. pin_number = code_value();
  928. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  929. {
  930. if (sensitive_pins[i] == pin_number)
  931. {
  932. pin_number = -1;
  933. break;
  934. }
  935. }
  936. #if FAN_PIN > 0
  937. if (pin_number == FAN_PIN)
  938. fanSpeed = pin_status;
  939. #endif
  940. if (pin_number > -1)
  941. {
  942. pinMode(pin_number, OUTPUT);
  943. digitalWrite(pin_number, pin_status);
  944. analogWrite(pin_number, pin_status);
  945. }
  946. }
  947. break;
  948. case 104: // M104
  949. if(setTargetedHotend(104)){
  950. break;
  951. }
  952. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  953. setWatch();
  954. break;
  955. case 140: // M140 set bed temp
  956. if (code_seen('S')) setTargetBed(code_value());
  957. break;
  958. case 105 : // M105
  959. if(setTargetedHotend(105)){
  960. break;
  961. }
  962. #if (TEMP_0_PIN > 0)
  963. SERIAL_PROTOCOLPGM("ok T:");
  964. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  965. SERIAL_PROTOCOLPGM(" /");
  966. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  967. #if TEMP_BED_PIN > 0
  968. SERIAL_PROTOCOLPGM(" B:");
  969. SERIAL_PROTOCOL_F(degBed(),1);
  970. SERIAL_PROTOCOLPGM(" /");
  971. SERIAL_PROTOCOL_F(degTargetBed(),1);
  972. #endif //TEMP_BED_PIN
  973. #else
  974. SERIAL_ERROR_START;
  975. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  976. #endif
  977. SERIAL_PROTOCOLPGM(" @:");
  978. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  979. SERIAL_PROTOCOLPGM(" B@:");
  980. SERIAL_PROTOCOL(getHeaterPower(-1));
  981. SERIAL_PROTOCOLLN("");
  982. return;
  983. break;
  984. case 109:
  985. {// M109 - Wait for extruder heater to reach target.
  986. if(setTargetedHotend(109)){
  987. break;
  988. }
  989. LCD_MESSAGEPGM(MSG_HEATING);
  990. #ifdef AUTOTEMP
  991. autotemp_enabled=false;
  992. #endif
  993. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  994. #ifdef AUTOTEMP
  995. if (code_seen('S')) autotemp_min=code_value();
  996. if (code_seen('B')) autotemp_max=code_value();
  997. if (code_seen('F'))
  998. {
  999. autotemp_factor=code_value();
  1000. autotemp_enabled=true;
  1001. }
  1002. #endif
  1003. setWatch();
  1004. codenum = millis();
  1005. /* See if we are heating up or cooling down */
  1006. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  1007. #ifdef TEMP_RESIDENCY_TIME
  1008. long residencyStart;
  1009. residencyStart = -1;
  1010. /* continue to loop until we have reached the target temp
  1011. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  1012. while((residencyStart == -1) ||
  1013. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  1014. #else
  1015. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  1016. #endif //TEMP_RESIDENCY_TIME
  1017. if( (millis() - codenum) > 1000UL )
  1018. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  1019. SERIAL_PROTOCOLPGM("T:");
  1020. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1021. SERIAL_PROTOCOLPGM(" E:");
  1022. SERIAL_PROTOCOL((int)tmp_extruder);
  1023. #ifdef TEMP_RESIDENCY_TIME
  1024. SERIAL_PROTOCOLPGM(" W:");
  1025. if(residencyStart > -1)
  1026. {
  1027. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  1028. SERIAL_PROTOCOLLN( codenum );
  1029. }
  1030. else
  1031. {
  1032. SERIAL_PROTOCOLLN( "?" );
  1033. }
  1034. #else
  1035. SERIAL_PROTOCOLLN("");
  1036. #endif
  1037. codenum = millis();
  1038. }
  1039. manage_heater();
  1040. manage_inactivity();
  1041. lcd_update();
  1042. #ifdef TEMP_RESIDENCY_TIME
  1043. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  1044. or when current temp falls outside the hysteresis after target temp was reached */
  1045. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  1046. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  1047. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  1048. {
  1049. residencyStart = millis();
  1050. }
  1051. #endif //TEMP_RESIDENCY_TIME
  1052. }
  1053. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  1054. starttime=millis();
  1055. previous_millis_cmd = millis();
  1056. }
  1057. break;
  1058. case 190: // M190 - Wait for bed heater to reach target.
  1059. #if TEMP_BED_PIN > 0
  1060. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1061. if (code_seen('S')) setTargetBed(code_value());
  1062. codenum = millis();
  1063. while(isHeatingBed())
  1064. {
  1065. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  1066. {
  1067. float tt=degHotend(active_extruder);
  1068. SERIAL_PROTOCOLPGM("T:");
  1069. SERIAL_PROTOCOL(tt);
  1070. SERIAL_PROTOCOLPGM(" E:");
  1071. SERIAL_PROTOCOL((int)active_extruder);
  1072. SERIAL_PROTOCOLPGM(" B:");
  1073. SERIAL_PROTOCOL_F(degBed(),1);
  1074. SERIAL_PROTOCOLLN("");
  1075. codenum = millis();
  1076. }
  1077. manage_heater();
  1078. manage_inactivity();
  1079. lcd_update();
  1080. }
  1081. LCD_MESSAGEPGM(MSG_BED_DONE);
  1082. previous_millis_cmd = millis();
  1083. #endif
  1084. break;
  1085. #if FAN_PIN > 0
  1086. case 106: //M106 Fan On
  1087. if (code_seen('S')){
  1088. fanSpeed=constrain(code_value(),0,255);
  1089. }
  1090. else {
  1091. fanSpeed=255;
  1092. }
  1093. break;
  1094. case 107: //M107 Fan Off
  1095. fanSpeed = 0;
  1096. break;
  1097. #endif //FAN_PIN
  1098. #ifdef BARICUDA
  1099. // PWM for HEATER_1_PIN
  1100. #if HEATER_1_PIN > 0
  1101. case 126: //M126 valve open
  1102. if (code_seen('S')){
  1103. ValvePressure=constrain(code_value(),0,255);
  1104. }
  1105. else {
  1106. ValvePressure=255;
  1107. }
  1108. break;
  1109. case 127: //M127 valve closed
  1110. ValvePressure = 0;
  1111. break;
  1112. #endif //HEATER_1_PIN
  1113. // PWM for HEATER_2_PIN
  1114. #if HEATER_2_PIN > 0
  1115. case 128: //M128 valve open
  1116. if (code_seen('S')){
  1117. EtoPPressure=constrain(code_value(),0,255);
  1118. }
  1119. else {
  1120. EtoPPressure=255;
  1121. }
  1122. break;
  1123. case 129: //M129 valve closed
  1124. EtoPPressure = 0;
  1125. break;
  1126. #endif //HEATER_2_PIN
  1127. #endif
  1128. #if (PS_ON_PIN > 0)
  1129. case 80: // M80 - ATX Power On
  1130. SET_OUTPUT(PS_ON_PIN); //GND
  1131. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  1132. break;
  1133. #endif
  1134. case 81: // M81 - ATX Power Off
  1135. #if defined SUICIDE_PIN && SUICIDE_PIN > 0
  1136. st_synchronize();
  1137. suicide();
  1138. #elif (PS_ON_PIN > 0)
  1139. SET_OUTPUT(PS_ON_PIN);
  1140. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  1141. #endif
  1142. break;
  1143. case 82:
  1144. axis_relative_modes[3] = false;
  1145. break;
  1146. case 83:
  1147. axis_relative_modes[3] = true;
  1148. break;
  1149. case 18: //compatibility
  1150. case 84: // M84
  1151. if(code_seen('S')){
  1152. stepper_inactive_time = code_value() * 1000;
  1153. }
  1154. else
  1155. {
  1156. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1157. if(all_axis)
  1158. {
  1159. st_synchronize();
  1160. disable_e0();
  1161. disable_e1();
  1162. disable_e2();
  1163. finishAndDisableSteppers();
  1164. }
  1165. else
  1166. {
  1167. st_synchronize();
  1168. if(code_seen('X')) disable_x();
  1169. if(code_seen('Y')) disable_y();
  1170. if(code_seen('Z')) disable_z();
  1171. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1172. if(code_seen('E')) {
  1173. disable_e0();
  1174. disable_e1();
  1175. disable_e2();
  1176. }
  1177. #endif
  1178. }
  1179. }
  1180. break;
  1181. case 85: // M85
  1182. code_seen('S');
  1183. max_inactive_time = code_value() * 1000;
  1184. break;
  1185. case 92: // M92
  1186. for(int8_t i=0; i < NUM_AXIS; i++)
  1187. {
  1188. if(code_seen(axis_codes[i]))
  1189. {
  1190. if(i == 3) { // E
  1191. float value = code_value();
  1192. if(value < 20.0) {
  1193. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1194. max_e_jerk *= factor;
  1195. max_feedrate[i] *= factor;
  1196. axis_steps_per_sqr_second[i] *= factor;
  1197. }
  1198. axis_steps_per_unit[i] = value;
  1199. }
  1200. else {
  1201. axis_steps_per_unit[i] = code_value();
  1202. }
  1203. }
  1204. }
  1205. break;
  1206. case 115: // M115
  1207. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  1208. break;
  1209. case 117: // M117 display message
  1210. starpos = (strchr(strchr_pointer + 5,'*'));
  1211. if(starpos!=NULL)
  1212. *(starpos-1)='\0';
  1213. lcd_setstatus(strchr_pointer + 5);
  1214. break;
  1215. case 114: // M114
  1216. SERIAL_PROTOCOLPGM("X:");
  1217. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1218. SERIAL_PROTOCOLPGM("Y:");
  1219. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1220. SERIAL_PROTOCOLPGM("Z:");
  1221. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1222. SERIAL_PROTOCOLPGM("E:");
  1223. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1224. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1225. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1226. SERIAL_PROTOCOLPGM("Y:");
  1227. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1228. SERIAL_PROTOCOLPGM("Z:");
  1229. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1230. SERIAL_PROTOCOLLN("");
  1231. break;
  1232. case 120: // M120
  1233. enable_endstops(false) ;
  1234. break;
  1235. case 121: // M121
  1236. enable_endstops(true) ;
  1237. break;
  1238. case 119: // M119
  1239. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1240. #if (X_MIN_PIN > 0)
  1241. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1242. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1243. #endif
  1244. #if (X_MAX_PIN > 0)
  1245. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1246. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1247. #endif
  1248. #if (Y_MIN_PIN > 0)
  1249. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1250. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1251. #endif
  1252. #if (Y_MAX_PIN > 0)
  1253. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1254. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1255. #endif
  1256. #if (Z_MIN_PIN > 0)
  1257. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1258. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1259. #endif
  1260. #if (Z_MAX_PIN > 0)
  1261. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1262. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1263. #endif
  1264. break;
  1265. //TODO: update for all axis, use for loop
  1266. case 201: // M201
  1267. for(int8_t i=0; i < NUM_AXIS; i++)
  1268. {
  1269. if(code_seen(axis_codes[i]))
  1270. {
  1271. max_acceleration_units_per_sq_second[i] = code_value();
  1272. }
  1273. }
  1274. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  1275. reset_acceleration_rates();
  1276. break;
  1277. #if 0 // Not used for Sprinter/grbl gen6
  1278. case 202: // M202
  1279. for(int8_t i=0; i < NUM_AXIS; i++) {
  1280. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1281. }
  1282. break;
  1283. #endif
  1284. case 203: // M203 max feedrate mm/sec
  1285. for(int8_t i=0; i < NUM_AXIS; i++) {
  1286. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1287. }
  1288. break;
  1289. case 204: // M204 acclereration S normal moves T filmanent only moves
  1290. {
  1291. if(code_seen('S')) acceleration = code_value() ;
  1292. if(code_seen('T')) retract_acceleration = code_value() ;
  1293. }
  1294. break;
  1295. 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
  1296. {
  1297. if(code_seen('S')) minimumfeedrate = code_value();
  1298. if(code_seen('T')) mintravelfeedrate = code_value();
  1299. if(code_seen('B')) minsegmenttime = code_value() ;
  1300. if(code_seen('X')) max_xy_jerk = code_value() ;
  1301. if(code_seen('Z')) max_z_jerk = code_value() ;
  1302. if(code_seen('E')) max_e_jerk = code_value() ;
  1303. }
  1304. break;
  1305. case 206: // M206 additional homeing offset
  1306. for(int8_t i=0; i < 3; i++)
  1307. {
  1308. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1309. }
  1310. break;
  1311. #ifdef FWRETRACT
  1312. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1313. {
  1314. if(code_seen('S'))
  1315. {
  1316. retract_length = code_value() ;
  1317. }
  1318. if(code_seen('F'))
  1319. {
  1320. retract_feedrate = code_value() ;
  1321. }
  1322. if(code_seen('Z'))
  1323. {
  1324. retract_zlift = code_value() ;
  1325. }
  1326. }break;
  1327. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1328. {
  1329. if(code_seen('S'))
  1330. {
  1331. retract_recover_length = code_value() ;
  1332. }
  1333. if(code_seen('F'))
  1334. {
  1335. retract_recover_feedrate = code_value() ;
  1336. }
  1337. }break;
  1338. 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.
  1339. {
  1340. if(code_seen('S'))
  1341. {
  1342. int t= code_value() ;
  1343. switch(t)
  1344. {
  1345. case 0: autoretract_enabled=false;retracted=false;break;
  1346. case 1: autoretract_enabled=true;retracted=false;break;
  1347. default:
  1348. SERIAL_ECHO_START;
  1349. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1350. SERIAL_ECHO(cmdbuffer[bufindr]);
  1351. SERIAL_ECHOLNPGM("\"");
  1352. }
  1353. }
  1354. }break;
  1355. #endif // FWRETRACT
  1356. #if EXTRUDERS > 1
  1357. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  1358. {
  1359. if(setTargetedHotend(218)){
  1360. break;
  1361. }
  1362. if(code_seen('X'))
  1363. {
  1364. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  1365. }
  1366. if(code_seen('Y'))
  1367. {
  1368. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  1369. }
  1370. SERIAL_ECHO_START;
  1371. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1372. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  1373. {
  1374. SERIAL_ECHO(" ");
  1375. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  1376. SERIAL_ECHO(",");
  1377. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  1378. }
  1379. SERIAL_ECHOLN("");
  1380. }break;
  1381. #endif
  1382. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1383. {
  1384. if(code_seen('S'))
  1385. {
  1386. feedmultiply = code_value() ;
  1387. }
  1388. }
  1389. break;
  1390. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1391. {
  1392. if(code_seen('S'))
  1393. {
  1394. extrudemultiply = code_value() ;
  1395. }
  1396. }
  1397. break;
  1398. #if NUM_SERVOS > 0
  1399. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  1400. {
  1401. int servo_index = -1;
  1402. int servo_position = 0;
  1403. if (code_seen('P'))
  1404. servo_index = code_value();
  1405. if (code_seen('S')) {
  1406. servo_position = code_value();
  1407. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  1408. servos[servo_index].write(servo_position);
  1409. }
  1410. else {
  1411. SERIAL_ECHO_START;
  1412. SERIAL_ECHO("Servo ");
  1413. SERIAL_ECHO(servo_index);
  1414. SERIAL_ECHOLN(" out of range");
  1415. }
  1416. }
  1417. else if (servo_index >= 0) {
  1418. SERIAL_PROTOCOL(MSG_OK);
  1419. SERIAL_PROTOCOL(" Servo ");
  1420. SERIAL_PROTOCOL(servo_index);
  1421. SERIAL_PROTOCOL(": ");
  1422. SERIAL_PROTOCOL(servos[servo_index].read());
  1423. SERIAL_PROTOCOLLN("");
  1424. }
  1425. }
  1426. break;
  1427. #endif // NUM_SERVOS > 0
  1428. #if LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) )
  1429. case 300: // M300
  1430. {
  1431. int beepS = 400;
  1432. int beepP = 1000;
  1433. if(code_seen('S')) beepS = code_value();
  1434. if(code_seen('P')) beepP = code_value();
  1435. #if BEEPER > 0
  1436. tone(BEEPER, beepS);
  1437. delay(beepP);
  1438. noTone(BEEPER);
  1439. #elif defined(ULTRALCD)
  1440. lcd_buzz(beepS, beepP);
  1441. #endif
  1442. }
  1443. break;
  1444. #endif // M300
  1445. #ifdef PIDTEMP
  1446. case 301: // M301
  1447. {
  1448. if(code_seen('P')) Kp = code_value();
  1449. if(code_seen('I')) Ki = scalePID_i(code_value());
  1450. if(code_seen('D')) Kd = scalePID_d(code_value());
  1451. #ifdef PID_ADD_EXTRUSION_RATE
  1452. if(code_seen('C')) Kc = code_value();
  1453. #endif
  1454. updatePID();
  1455. SERIAL_PROTOCOL(MSG_OK);
  1456. SERIAL_PROTOCOL(" p:");
  1457. SERIAL_PROTOCOL(Kp);
  1458. SERIAL_PROTOCOL(" i:");
  1459. SERIAL_PROTOCOL(unscalePID_i(Ki));
  1460. SERIAL_PROTOCOL(" d:");
  1461. SERIAL_PROTOCOL(unscalePID_d(Kd));
  1462. #ifdef PID_ADD_EXTRUSION_RATE
  1463. SERIAL_PROTOCOL(" c:");
  1464. //Kc does not have scaling applied above, or in resetting defaults
  1465. SERIAL_PROTOCOL(Kc);
  1466. #endif
  1467. SERIAL_PROTOCOLLN("");
  1468. }
  1469. break;
  1470. #endif //PIDTEMP
  1471. #ifdef PIDTEMPBED
  1472. case 304: // M304
  1473. {
  1474. if(code_seen('P')) bedKp = code_value();
  1475. if(code_seen('I')) bedKi = scalePID_i(code_value());
  1476. if(code_seen('D')) bedKd = scalePID_d(code_value());
  1477. updatePID();
  1478. SERIAL_PROTOCOL(MSG_OK);
  1479. SERIAL_PROTOCOL(" p:");
  1480. SERIAL_PROTOCOL(bedKp);
  1481. SERIAL_PROTOCOL(" i:");
  1482. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  1483. SERIAL_PROTOCOL(" d:");
  1484. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  1485. SERIAL_PROTOCOLLN("");
  1486. }
  1487. break;
  1488. #endif //PIDTEMP
  1489. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1490. {
  1491. #ifdef PHOTOGRAPH_PIN
  1492. #if (PHOTOGRAPH_PIN > 0)
  1493. const uint8_t NUM_PULSES=16;
  1494. const float PULSE_LENGTH=0.01524;
  1495. for(int i=0; i < NUM_PULSES; i++) {
  1496. WRITE(PHOTOGRAPH_PIN, HIGH);
  1497. _delay_ms(PULSE_LENGTH);
  1498. WRITE(PHOTOGRAPH_PIN, LOW);
  1499. _delay_ms(PULSE_LENGTH);
  1500. }
  1501. delay(7.33);
  1502. for(int i=0; i < NUM_PULSES; i++) {
  1503. WRITE(PHOTOGRAPH_PIN, HIGH);
  1504. _delay_ms(PULSE_LENGTH);
  1505. WRITE(PHOTOGRAPH_PIN, LOW);
  1506. _delay_ms(PULSE_LENGTH);
  1507. }
  1508. #endif
  1509. #endif
  1510. }
  1511. break;
  1512. case 302: // allow cold extrudes
  1513. {
  1514. allow_cold_extrudes(true);
  1515. }
  1516. break;
  1517. case 303: // M303 PID autotune
  1518. {
  1519. float temp = 150.0;
  1520. int e=0;
  1521. int c=5;
  1522. if (code_seen('E')) e=code_value();
  1523. if (e<0)
  1524. temp=70;
  1525. if (code_seen('S')) temp=code_value();
  1526. if (code_seen('C')) c=code_value();
  1527. PID_autotune(temp, e, c);
  1528. }
  1529. break;
  1530. case 400: // M400 finish all moves
  1531. {
  1532. st_synchronize();
  1533. }
  1534. break;
  1535. case 500: // M500 Store settings in EEPROM
  1536. {
  1537. Config_StoreSettings();
  1538. }
  1539. break;
  1540. case 501: // M501 Read settings from EEPROM
  1541. {
  1542. Config_RetrieveSettings();
  1543. }
  1544. break;
  1545. case 502: // M502 Revert to default settings
  1546. {
  1547. Config_ResetDefault();
  1548. }
  1549. break;
  1550. case 503: // M503 print settings currently in memory
  1551. {
  1552. Config_PrintSettings();
  1553. }
  1554. break;
  1555. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  1556. case 540:
  1557. {
  1558. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  1559. }
  1560. break;
  1561. #endif
  1562. #ifdef FILAMENTCHANGEENABLE
  1563. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  1564. {
  1565. float target[4];
  1566. float lastpos[4];
  1567. target[X_AXIS]=current_position[X_AXIS];
  1568. target[Y_AXIS]=current_position[Y_AXIS];
  1569. target[Z_AXIS]=current_position[Z_AXIS];
  1570. target[E_AXIS]=current_position[E_AXIS];
  1571. lastpos[X_AXIS]=current_position[X_AXIS];
  1572. lastpos[Y_AXIS]=current_position[Y_AXIS];
  1573. lastpos[Z_AXIS]=current_position[Z_AXIS];
  1574. lastpos[E_AXIS]=current_position[E_AXIS];
  1575. //retract by E
  1576. if(code_seen('E'))
  1577. {
  1578. target[E_AXIS]+= code_value();
  1579. }
  1580. else
  1581. {
  1582. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  1583. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  1584. #endif
  1585. }
  1586. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1587. //lift Z
  1588. if(code_seen('Z'))
  1589. {
  1590. target[Z_AXIS]+= code_value();
  1591. }
  1592. else
  1593. {
  1594. #ifdef FILAMENTCHANGE_ZADD
  1595. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  1596. #endif
  1597. }
  1598. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1599. //move xy
  1600. if(code_seen('X'))
  1601. {
  1602. target[X_AXIS]+= code_value();
  1603. }
  1604. else
  1605. {
  1606. #ifdef FILAMENTCHANGE_XPOS
  1607. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  1608. #endif
  1609. }
  1610. if(code_seen('Y'))
  1611. {
  1612. target[Y_AXIS]= code_value();
  1613. }
  1614. else
  1615. {
  1616. #ifdef FILAMENTCHANGE_YPOS
  1617. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  1618. #endif
  1619. }
  1620. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1621. if(code_seen('L'))
  1622. {
  1623. target[E_AXIS]+= code_value();
  1624. }
  1625. else
  1626. {
  1627. #ifdef FILAMENTCHANGE_FINALRETRACT
  1628. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  1629. #endif
  1630. }
  1631. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1632. //finish moves
  1633. st_synchronize();
  1634. //disable extruder steppers so filament can be removed
  1635. disable_e0();
  1636. disable_e1();
  1637. disable_e2();
  1638. delay(100);
  1639. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  1640. uint8_t cnt=0;
  1641. while(!lcd_clicked()){
  1642. cnt++;
  1643. manage_heater();
  1644. manage_inactivity();
  1645. lcd_update();
  1646. if(cnt==0)
  1647. {
  1648. #if BEEPER > 0
  1649. SET_OUTPUT(BEEPER);
  1650. WRITE(BEEPER,HIGH);
  1651. delay(3);
  1652. WRITE(BEEPER,LOW);
  1653. delay(3);
  1654. #else
  1655. lcd_buzz(1000/6,100);
  1656. #endif
  1657. }
  1658. }
  1659. //return to normal
  1660. if(code_seen('L'))
  1661. {
  1662. target[E_AXIS]+= -code_value();
  1663. }
  1664. else
  1665. {
  1666. #ifdef FILAMENTCHANGE_FINALRETRACT
  1667. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  1668. #endif
  1669. }
  1670. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  1671. plan_set_e_position(current_position[E_AXIS]);
  1672. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //should do nothing
  1673. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move xy back
  1674. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move z back
  1675. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], feedrate/60, active_extruder); //final untretract
  1676. }
  1677. break;
  1678. #endif //FILAMENTCHANGEENABLE
  1679. case 907: // M907 Set digital trimpot motor current using axis codes.
  1680. {
  1681. #if DIGIPOTSS_PIN > 0
  1682. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  1683. if(code_seen('B')) digipot_current(4,code_value());
  1684. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  1685. #endif
  1686. }
  1687. break;
  1688. case 908: // M908 Control digital trimpot directly.
  1689. {
  1690. #if DIGIPOTSS_PIN > 0
  1691. uint8_t channel,current;
  1692. if(code_seen('P')) channel=code_value();
  1693. if(code_seen('S')) current=code_value();
  1694. digitalPotWrite(channel, current);
  1695. #endif
  1696. }
  1697. break;
  1698. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  1699. {
  1700. #if X_MS1_PIN > 0
  1701. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  1702. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  1703. if(code_seen('B')) microstep_mode(4,code_value());
  1704. microstep_readings();
  1705. #endif
  1706. }
  1707. break;
  1708. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  1709. {
  1710. #if X_MS1_PIN > 0
  1711. if(code_seen('S')) switch((int)code_value())
  1712. {
  1713. case 1:
  1714. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  1715. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  1716. break;
  1717. case 2:
  1718. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  1719. if(code_seen('B')) microstep_ms(4,-1,code_value());
  1720. break;
  1721. }
  1722. microstep_readings();
  1723. #endif
  1724. }
  1725. break;
  1726. case 999: // M999: Restart after being stopped
  1727. Stopped = false;
  1728. lcd_reset_alert_level();
  1729. gcode_LastN = Stopped_gcode_LastN;
  1730. FlushSerialRequestResend();
  1731. break;
  1732. }
  1733. }
  1734. else if(code_seen('T'))
  1735. {
  1736. tmp_extruder = code_value();
  1737. if(tmp_extruder >= EXTRUDERS) {
  1738. SERIAL_ECHO_START;
  1739. SERIAL_ECHO("T");
  1740. SERIAL_ECHO(tmp_extruder);
  1741. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1742. }
  1743. else {
  1744. boolean make_move = false;
  1745. if(code_seen('F')) {
  1746. make_move = true;
  1747. next_feedrate = code_value();
  1748. if(next_feedrate > 0.0) {
  1749. feedrate = next_feedrate;
  1750. }
  1751. }
  1752. #if EXTRUDERS > 1
  1753. if(tmp_extruder != active_extruder) {
  1754. // Save current position to return to after applying extruder offset
  1755. memcpy(destination, current_position, sizeof(destination));
  1756. // Offset extruder (only by XY)
  1757. int i;
  1758. for(i = 0; i < 2; i++) {
  1759. current_position[i] = current_position[i] -
  1760. extruder_offset[i][active_extruder] +
  1761. extruder_offset[i][tmp_extruder];
  1762. }
  1763. // Set the new active extruder and position
  1764. active_extruder = tmp_extruder;
  1765. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1766. // Move to the old position if 'F' was in the parameters
  1767. if(make_move && Stopped == false) {
  1768. prepare_move();
  1769. }
  1770. }
  1771. #endif
  1772. SERIAL_ECHO_START;
  1773. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1774. SERIAL_PROTOCOLLN((int)active_extruder);
  1775. }
  1776. }
  1777. else
  1778. {
  1779. SERIAL_ECHO_START;
  1780. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1781. SERIAL_ECHO(cmdbuffer[bufindr]);
  1782. SERIAL_ECHOLNPGM("\"");
  1783. }
  1784. ClearToSend();
  1785. }
  1786. void FlushSerialRequestResend()
  1787. {
  1788. //char cmdbuffer[bufindr][100]="Resend:";
  1789. MYSERIAL.flush();
  1790. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1791. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1792. ClearToSend();
  1793. }
  1794. void ClearToSend()
  1795. {
  1796. previous_millis_cmd = millis();
  1797. #ifdef SDSUPPORT
  1798. if(fromsd[bufindr])
  1799. return;
  1800. #endif //SDSUPPORT
  1801. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1802. }
  1803. void get_coordinates()
  1804. {
  1805. bool seen[4]={false,false,false,false};
  1806. for(int8_t i=0; i < NUM_AXIS; i++) {
  1807. if(code_seen(axis_codes[i]))
  1808. {
  1809. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1810. seen[i]=true;
  1811. }
  1812. else destination[i] = current_position[i]; //Are these else lines really needed?
  1813. }
  1814. if(code_seen('F')) {
  1815. next_feedrate = code_value();
  1816. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1817. }
  1818. #ifdef FWRETRACT
  1819. if(autoretract_enabled)
  1820. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1821. {
  1822. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1823. if(echange<-MIN_RETRACT) //retract
  1824. {
  1825. if(!retracted)
  1826. {
  1827. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1828. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1829. float correctede=-echange-retract_length;
  1830. //to generate the additional steps, not the destination is changed, but inversely the current position
  1831. current_position[E_AXIS]+=-correctede;
  1832. feedrate=retract_feedrate;
  1833. retracted=true;
  1834. }
  1835. }
  1836. else
  1837. if(echange>MIN_RETRACT) //retract_recover
  1838. {
  1839. if(retracted)
  1840. {
  1841. //current_position[Z_AXIS]+=-retract_zlift;
  1842. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1843. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1844. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1845. feedrate=retract_recover_feedrate;
  1846. retracted=false;
  1847. }
  1848. }
  1849. }
  1850. #endif //FWRETRACT
  1851. }
  1852. void get_arc_coordinates()
  1853. {
  1854. #ifdef SF_ARC_FIX
  1855. bool relative_mode_backup = relative_mode;
  1856. relative_mode = true;
  1857. #endif
  1858. get_coordinates();
  1859. #ifdef SF_ARC_FIX
  1860. relative_mode=relative_mode_backup;
  1861. #endif
  1862. if(code_seen('I')) {
  1863. offset[0] = code_value();
  1864. }
  1865. else {
  1866. offset[0] = 0.0;
  1867. }
  1868. if(code_seen('J')) {
  1869. offset[1] = code_value();
  1870. }
  1871. else {
  1872. offset[1] = 0.0;
  1873. }
  1874. }
  1875. void clamp_to_software_endstops(float target[3])
  1876. {
  1877. if (min_software_endstops) {
  1878. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  1879. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  1880. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  1881. }
  1882. if (max_software_endstops) {
  1883. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  1884. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  1885. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  1886. }
  1887. }
  1888. void prepare_move()
  1889. {
  1890. clamp_to_software_endstops(destination);
  1891. previous_millis_cmd = millis();
  1892. // Do not use feedmultiply for E or Z only moves
  1893. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  1894. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1895. }
  1896. else {
  1897. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1898. }
  1899. for(int8_t i=0; i < NUM_AXIS; i++) {
  1900. current_position[i] = destination[i];
  1901. }
  1902. }
  1903. void prepare_arc_move(char isclockwise) {
  1904. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1905. // Trace the arc
  1906. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1907. // As far as the parser is concerned, the position is now == target. In reality the
  1908. // motion control system might still be processing the action and the real tool position
  1909. // in any intermediate location.
  1910. for(int8_t i=0; i < NUM_AXIS; i++) {
  1911. current_position[i] = destination[i];
  1912. }
  1913. previous_millis_cmd = millis();
  1914. }
  1915. #if CONTROLLERFAN_PIN > 0
  1916. #if CONTROLLERFAN_PIN == FAN_PIN
  1917. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  1918. #endif
  1919. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1920. unsigned long lastMotorCheck = 0;
  1921. void controllerFan()
  1922. {
  1923. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1924. {
  1925. lastMotorCheck = millis();
  1926. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1927. #if EXTRUDERS > 2
  1928. || !READ(E2_ENABLE_PIN)
  1929. #endif
  1930. #if EXTRUDER > 1
  1931. || !READ(E1_ENABLE_PIN)
  1932. #endif
  1933. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1934. {
  1935. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1936. }
  1937. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1938. {
  1939. digitalWrite(CONTROLLERFAN_PIN, 0);
  1940. analogWrite(CONTROLLERFAN_PIN, 0);
  1941. }
  1942. else
  1943. {
  1944. // allows digital or PWM fan output to be used (see M42 handling)
  1945. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  1946. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  1947. }
  1948. }
  1949. }
  1950. #endif
  1951. void manage_inactivity()
  1952. {
  1953. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1954. if(max_inactive_time)
  1955. kill();
  1956. if(stepper_inactive_time) {
  1957. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1958. {
  1959. if(blocks_queued() == false) {
  1960. disable_x();
  1961. disable_y();
  1962. disable_z();
  1963. disable_e0();
  1964. disable_e1();
  1965. disable_e2();
  1966. }
  1967. }
  1968. }
  1969. #if KILL_PIN > 0
  1970. if( 0 == READ(KILL_PIN) )
  1971. kill();
  1972. #endif
  1973. #if CONTROLLERFAN_PIN > 0
  1974. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1975. #endif
  1976. #ifdef EXTRUDER_RUNOUT_PREVENT
  1977. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1978. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1979. {
  1980. bool oldstatus=READ(E0_ENABLE_PIN);
  1981. enable_e0();
  1982. float oldepos=current_position[E_AXIS];
  1983. float oldedes=destination[E_AXIS];
  1984. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1985. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1986. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1987. current_position[E_AXIS]=oldepos;
  1988. destination[E_AXIS]=oldedes;
  1989. plan_set_e_position(oldepos);
  1990. previous_millis_cmd=millis();
  1991. st_synchronize();
  1992. WRITE(E0_ENABLE_PIN,oldstatus);
  1993. }
  1994. #endif
  1995. check_axes_activity();
  1996. }
  1997. void kill()
  1998. {
  1999. cli(); // Stop interrupts
  2000. disable_heater();
  2001. disable_x();
  2002. disable_y();
  2003. disable_z();
  2004. disable_e0();
  2005. disable_e1();
  2006. disable_e2();
  2007. if(PS_ON_PIN > 0) pinMode(PS_ON_PIN,INPUT);
  2008. SERIAL_ERROR_START;
  2009. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  2010. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  2011. suicide();
  2012. while(1) { /* Intentionally left empty */ } // Wait for reset
  2013. }
  2014. void Stop()
  2015. {
  2016. disable_heater();
  2017. if(Stopped == false) {
  2018. Stopped = true;
  2019. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  2020. SERIAL_ERROR_START;
  2021. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  2022. LCD_MESSAGEPGM(MSG_STOPPED);
  2023. }
  2024. }
  2025. bool IsStopped() { return Stopped; };
  2026. #ifdef FAST_PWM_FAN
  2027. void setPwmFrequency(uint8_t pin, int val)
  2028. {
  2029. val &= 0x07;
  2030. switch(digitalPinToTimer(pin))
  2031. {
  2032. #if defined(TCCR0A)
  2033. case TIMER0A:
  2034. case TIMER0B:
  2035. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  2036. // TCCR0B |= val;
  2037. break;
  2038. #endif
  2039. #if defined(TCCR1A)
  2040. case TIMER1A:
  2041. case TIMER1B:
  2042. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2043. // TCCR1B |= val;
  2044. break;
  2045. #endif
  2046. #if defined(TCCR2)
  2047. case TIMER2:
  2048. case TIMER2:
  2049. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2050. TCCR2 |= val;
  2051. break;
  2052. #endif
  2053. #if defined(TCCR2A)
  2054. case TIMER2A:
  2055. case TIMER2B:
  2056. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  2057. TCCR2B |= val;
  2058. break;
  2059. #endif
  2060. #if defined(TCCR3A)
  2061. case TIMER3A:
  2062. case TIMER3B:
  2063. case TIMER3C:
  2064. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  2065. TCCR3B |= val;
  2066. break;
  2067. #endif
  2068. #if defined(TCCR4A)
  2069. case TIMER4A:
  2070. case TIMER4B:
  2071. case TIMER4C:
  2072. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  2073. TCCR4B |= val;
  2074. break;
  2075. #endif
  2076. #if defined(TCCR5A)
  2077. case TIMER5A:
  2078. case TIMER5B:
  2079. case TIMER5C:
  2080. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  2081. TCCR5B |= val;
  2082. break;
  2083. #endif
  2084. }
  2085. }
  2086. #endif //FAST_PWM_FAN
  2087. bool setTargetedHotend(int code){
  2088. tmp_extruder = active_extruder;
  2089. if(code_seen('T')) {
  2090. tmp_extruder = code_value();
  2091. if(tmp_extruder >= EXTRUDERS) {
  2092. SERIAL_ECHO_START;
  2093. switch(code){
  2094. case 104:
  2095. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  2096. break;
  2097. case 105:
  2098. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  2099. break;
  2100. case 109:
  2101. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  2102. break;
  2103. case 218:
  2104. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  2105. break;
  2106. }
  2107. SERIAL_ECHOLN(tmp_extruder);
  2108. return true;
  2109. }
  2110. }
  2111. return false;
  2112. }