12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935 |
- #include "Configuration.h"
- #include "Marlin.h"
- #include "config.h"
- #include "macros.h"
- #ifdef ENABLE_AUTO_BED_LEVELING
- #include "vector_3.h"
- #ifdef AUTO_BED_LEVELING_GRID
- #include "qr_solve.h"
- #endif
- #endif
- #ifdef MESH_BED_LEVELING
- #include "mesh_bed_leveling.h"
- #include "mesh_bed_calibration.h"
- #endif
- #include "printers.h"
- #include "menu.h"
- #include "ultralcd.h"
- #include "conv2str.h"
- #include "backlight.h"
- #include "planner.h"
- #include "stepper.h"
- #include "temperature.h"
- #include "motion_control.h"
- #include "cardreader.h"
- #include "ConfigurationStore.h"
- #include "language.h"
- #include "pins_arduino.h"
- #include "math.h"
- #include "util.h"
- #include "Timer.h"
- #include <avr/wdt.h>
- #include <avr/pgmspace.h>
- #include "Dcodes.h"
- #include "AutoDeplete.h"
- #ifndef LA_NOCOMPAT
- #include "la10compat.h"
- #endif
- #include "spi.h"
- #ifdef FILAMENT_SENSOR
- #include "fsensor.h"
- #ifdef IR_SENSOR
- #include "pat9125.h"
- #endif
- #endif
- #ifdef TMC2130
- #include "tmc2130.h"
- #endif
- #ifdef XFLASH
- #include "xflash.h"
- #include "optiboot_xflash.h"
- #endif
- #include "xflash_dump.h"
- #ifdef BLINKM
- #include "BlinkM.h"
- #include "Wire.h"
- #endif
- #ifdef ULTRALCD
- #include "ultralcd.h"
- #endif
- #if NUM_SERVOS > 0
- #include "Servo.h"
- #endif
- #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
- #include <SPI.h>
- #endif
- #include "mmu.h"
- #define VERSION_STRING "1.0.2"
- #include "ultralcd.h"
- #include "sound.h"
- #include "cmdqueue.h"
- #define FAN_PULSE_WIDTH_LIMIT ((fanSpeed > 100) ? 3 : 4)
- #define FILAMENT_DEFAULT 0
- #define FILAMENT_FLEX 1
- #define FILAMENT_PVA 2
- #define FILAMENT_UNDEFINED 255
- #ifdef SDSUPPORT
- CardReader card;
- #endif
- unsigned long PingTime = _millis();
- uint8_t mbl_z_probe_nr = 3;
- #define DEFAULT_RETRACTION 1
- #define DEFAULT_RETRACTION_MM 4
- float default_retraction = DEFAULT_RETRACTION;
- float homing_feedrate[] = HOMING_FEEDRATE;
- uint8_t axis_relative_modes = 0;
- int feedmultiply=100;
- int extrudemultiply=100;
- int extruder_multiply[EXTRUDERS] = {100
- #if EXTRUDERS > 1
- , 100
- #if EXTRUDERS > 2
- , 100
- #endif
- #endif
- };
- bool homing_flag = false;
- int8_t lcd_change_fil_state = 0;
- unsigned long pause_time = 0;
- unsigned long start_pause_print = _millis();
- unsigned long t_fan_rising_edge = _millis();
- LongTimer safetyTimer;
- static LongTimer crashDetTimer;
- bool mesh_bed_leveling_flag = false;
- #ifdef PRUSA_M28
- bool prusa_sd_card_upload = false;
- #endif
- uint8_t status_number = 0;
- unsigned long total_filament_used;
- HeatingStatus heating_status;
- uint8_t heating_status_counter;
- bool loading_flag = false;
- #define XY_NO_RESTORE_FLAG (mesh_bed_leveling_flag || homing_flag)
- bool fan_state[2];
- int fan_edge_counter[2];
- int fan_speed[2];
- float extruder_multiplier[EXTRUDERS] = {1.0
- #if EXTRUDERS > 1
- , 1.0
- #if EXTRUDERS > 2
- , 1.0
- #endif
- #endif
- };
- float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
- #define _x current_position[X_AXIS]
- #define _y current_position[Y_AXIS]
- #define _z current_position[Z_AXIS]
- #define _e current_position[E_AXIS]
- float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
- float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
- bool axis_known_position[3] = {false, false, false};
- #if EXTRUDERS > 1
- #define NUM_EXTRUDER_OFFSETS 2
- float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
- #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
- EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
- #endif
- };
- #endif
- uint8_t active_extruder = 0;
- int fanSpeed=0;
- uint8_t newFanSpeed = 0;
- #ifdef FWRETRACT
- bool retracted[EXTRUDERS]={false
- #if EXTRUDERS > 1
- , false
- #if EXTRUDERS > 2
- , false
- #endif
- #endif
- };
- bool retracted_swap[EXTRUDERS]={false
- #if EXTRUDERS > 1
- , false
- #if EXTRUDERS > 2
- , false
- #endif
- #endif
- };
- float retract_length_swap = RETRACT_LENGTH_SWAP;
- float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
- #endif
- #ifdef PS_DEFAULT_OFF
- bool powersupply = false;
- #else
- bool powersupply = true;
- #endif
- bool cancel_heatup = false;
- int8_t busy_state = NOT_BUSY;
- static long prev_busy_signal_ms = -1;
- uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL;
- const char errormagic[] PROGMEM = "Error:";
- const char echomagic[] PROGMEM = "echo:";
- const char G28W0[] PROGMEM = "G28 W0";
- bool no_response = false;
- uint8_t important_status;
- uint8_t saved_filament_type;
- #define X_COORD_INVALID (X_MIN_POS-1)
- #define Y_COORD_INVALID (Y_MIN_POS-1)
- #define SAVED_TARGET_UNSET X_COORD_INVALID
- float saved_target[NUM_AXIS] = {SAVED_TARGET_UNSET, 0, 0, 0};
- bool mmu_print_saved = false;
- uint8_t print_percent_done_normal = PRINT_PERCENT_DONE_INIT;
- uint8_t print_percent_done_silent = PRINT_PERCENT_DONE_INIT;
- uint16_t print_time_remaining_normal = PRINT_TIME_REMAINING_INIT;
- uint16_t print_time_remaining_silent = PRINT_TIME_REMAINING_INIT;
- uint16_t print_time_to_change_normal = PRINT_TIME_REMAINING_INIT;
- uint16_t print_time_to_change_silent = PRINT_TIME_REMAINING_INIT;
- uint32_t IP_address = 0;
- #define MSG_BED_LEVELING_FAILED_TIMEOUT 30
- const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
- float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
- static float offset[3] = {0.0, 0.0, 0.0};
- float feedrate = 1500.0;
- static float next_feedrate;
- static float saved_feedrate;
- const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
- static LongTimer previous_millis_cmd;
- unsigned long max_inactive_time = 0;
- static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
- static unsigned long safetytimer_inactive_time = DEFAULT_SAFETYTIMER_TIME_MINS*60*1000ul;
- unsigned long starttime=0;
- unsigned long stoptime=0;
- ShortTimer usb_timer;
- bool Stopped=false;
- #if NUM_SERVOS > 0
- Servo servos[NUM_SERVOS];
- #endif
- bool target_direction;
- #ifdef CHDK
- unsigned long chdkHigh = 0;
- bool chdkActive = false;
- #endif
- bool saved_printing = false;
- static uint32_t saved_sdpos = 0;
- uint8_t saved_printing_type = PRINTING_TYPE_SD;
- static float saved_pos[4] = { X_COORD_INVALID, 0, 0, 0 };
- static uint16_t saved_feedrate2 = 0;
- static int saved_feedmultiply2 = 0;
- static uint8_t saved_active_extruder = 0;
- static float saved_extruder_temperature = 0.0;
- static bool saved_extruder_relative_mode = false;
- static int saved_fanSpeed = 0;
- static int saved_feedmultiply_mm = 100;
- class AutoReportFeatures {
- union {
- struct {
- uint8_t temp : 1;
- uint8_t fans : 1;
- uint8_t pos: 1;
- uint8_t ar4 : 1;
- uint8_t ar5 : 1;
- uint8_t ar6 : 1;
- uint8_t ar7 : 1;
- } __attribute__((packed)) bits;
- uint8_t byte;
- } arFunctionsActive;
- uint8_t auto_report_period;
- public:
- LongTimer auto_report_timer;
- AutoReportFeatures():auto_report_period(0){
- #if defined(AUTO_REPORT)
- arFunctionsActive.byte = 0xff;
- #else
- arFunctionsActive.byte = 0;
- #endif
- }
-
- inline bool Temp()const { return arFunctionsActive.bits.temp != 0; }
- inline void SetTemp(uint8_t v){ arFunctionsActive.bits.temp = v; }
- inline bool Fans()const { return arFunctionsActive.bits.fans != 0; }
- inline void SetFans(uint8_t v){ arFunctionsActive.bits.fans = v; }
- inline bool Pos()const { return arFunctionsActive.bits.pos != 0; }
- inline void SetPos(uint8_t v){ arFunctionsActive.bits.pos = v; }
-
- inline void SetMask(uint8_t mask){ arFunctionsActive.byte = mask; }
-
-
-
- void SetPeriod(uint8_t p){
- auto_report_period = p;
- if (auto_report_period != 0){
- auto_report_timer.start();
- } else{
- auto_report_timer.stop();
- }
- }
-
- inline void TimerStart() { auto_report_timer.start(); }
- inline bool TimerRunning()const { return auto_report_timer.running(); }
- inline bool TimerExpired() { return auto_report_timer.expired(auto_report_period * 1000ul); }
- };
- AutoReportFeatures autoReportFeatures;
- static void get_arc_coordinates();
- static bool setTargetedHotend(int code, uint8_t &extruder);
- static void print_time_remaining_init();
- static void wait_for_heater(long codenum, uint8_t extruder);
- static void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis);
- static void gcode_M105(uint8_t extruder);
- #ifndef PINDA_THERMISTOR
- static void temp_compensation_start();
- static void temp_compensation_apply();
- #endif
- #ifdef PRUSA_SN_SUPPORT
- static uint8_t get_PRUSA_SN(char* SN);
- #endif
- uint16_t gcode_in_progress = 0;
- uint16_t mcode_in_progress = 0;
- void serial_echopair_P(const char *s_P, float v)
- { serialprintPGM(s_P); SERIAL_ECHO(v); }
- void serial_echopair_P(const char *s_P, double v)
- { serialprintPGM(s_P); SERIAL_ECHO(v); }
- void serial_echopair_P(const char *s_P, unsigned long v)
- { serialprintPGM(s_P); SERIAL_ECHO(v); }
- void serialprintPGM(const char *str) {
- while(uint8_t ch = pgm_read_byte(str)) {
- MYSERIAL.write((char)ch);
- ++str;
- }
- }
- void serialprintlnPGM(const char *str) {
- serialprintPGM(str);
- MYSERIAL.println();
- }
- #ifdef SDSUPPORT
- #include "SdFatUtil.h"
- int freeMemory() { return SdFatUtil::FreeRam(); }
- #else
- extern "C" {
- extern unsigned int __bss_end;
- extern unsigned int __heap_start;
- extern void *__brkval;
- int freeMemory() {
- int free_memory;
- if ((int)__brkval == 0)
- free_memory = ((int)&free_memory) - ((int)&__bss_end);
- else
- free_memory = ((int)&free_memory) - ((int)__brkval);
- return free_memory;
- }
- }
- #endif
- void setup_killpin()
- {
- #if defined(KILL_PIN) && KILL_PIN > -1
- SET_INPUT(KILL_PIN);
- WRITE(KILL_PIN,HIGH);
- #endif
- }
- void setup_homepin(void)
- {
- #if defined(HOME_PIN) && HOME_PIN > -1
- SET_INPUT(HOME_PIN);
- WRITE(HOME_PIN,HIGH);
- #endif
- }
- void setup_photpin()
- {
- #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
- SET_OUTPUT(PHOTOGRAPH_PIN);
- WRITE(PHOTOGRAPH_PIN, LOW);
- #endif
- }
- void setup_powerhold()
- {
- #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
- SET_OUTPUT(SUICIDE_PIN);
- WRITE(SUICIDE_PIN, HIGH);
- #endif
- #if defined(PS_ON_PIN) && PS_ON_PIN > -1
- SET_OUTPUT(PS_ON_PIN);
- #if defined(PS_DEFAULT_OFF)
- WRITE(PS_ON_PIN, PS_ON_ASLEEP);
- #else
- WRITE(PS_ON_PIN, PS_ON_AWAKE);
- #endif
- #endif
- }
- void suicide()
- {
- #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
- SET_OUTPUT(SUICIDE_PIN);
- WRITE(SUICIDE_PIN, LOW);
- #endif
- }
- void servo_init()
- {
- #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
- servos[0].attach(SERVO0_PIN);
- #endif
- #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
- servos[1].attach(SERVO1_PIN);
- #endif
- #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
- servos[2].attach(SERVO2_PIN);
- #endif
- #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
- servos[3].attach(SERVO3_PIN);
- #endif
- #if (NUM_SERVOS >= 5)
- #error "TODO: enter initalisation code for more servos"
- #endif
- }
- bool fans_check_enabled = true;
- #ifdef TMC2130
- void crashdet_stop_and_save_print()
- {
- stop_and_save_print_to_ram(10, -default_retraction);
- }
- void crashdet_restore_print_and_continue()
- {
- restore_print_from_ram_and_continue(default_retraction);
- }
- void crashdet_fmt_error(char* buf, uint8_t mask)
- {
- if(mask & X_AXIS_MASK) *buf++ = axis_codes[X_AXIS];
- if(mask & Y_AXIS_MASK) *buf++ = axis_codes[Y_AXIS];
- *buf++ = ' ';
- strcpy_P(buf, _T(MSG_CRASH_DETECTED));
- }
- void crashdet_detected(uint8_t mask)
- {
- st_synchronize();
- static uint8_t crashDet_counter = 0;
- static uint8_t crashDet_axes = 0;
- bool automatic_recovery_after_crash = true;
- char msg[LCD_WIDTH+1] = "";
- if (crashDetTimer.expired(CRASHDET_TIMER * 1000ul)) {
- crashDet_counter = 0;
- }
- if(++crashDet_counter >= CRASHDET_COUNTER_MAX) {
- automatic_recovery_after_crash = false;
- }
- crashDetTimer.start();
- crashDet_axes |= mask;
- lcd_update_enable(true);
- lcd_clear();
- lcd_update(2);
- if (mask & X_AXIS_MASK)
- {
- eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) + 1);
- eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) + 1);
- }
- if (mask & Y_AXIS_MASK)
- {
- eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) + 1);
- eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) + 1);
- }
- lcd_update_enable(true);
- lcd_update(2);
-
- crashdet_fmt_error(msg, mask);
- lcd_setstatus(msg);
- gcode_G28(true, true, false);
- st_synchronize();
- if (automatic_recovery_after_crash) {
- enquecommand_P(PSTR("CRASH_RECOVER"));
- }else{
- setTargetHotend(0, active_extruder);
-
- lcd_update_enable(false);
- lcd_clear();
- crashdet_fmt_error(msg, crashDet_axes);
- crashDet_axes = 0;
- lcd_print(msg);
-
- lcd_set_cursor(0, 1);
- lcd_puts_P(_T(MSG_RESUME_PRINT));
- lcd_putc('?');
- bool yesno = lcd_show_yes_no_and_wait(false);
- lcd_update_enable(true);
- if (yesno)
- {
- enquecommand_P(PSTR("CRASH_RECOVER"));
- }
- else
- {
- enquecommand_P(PSTR("CRASH_CANCEL"));
- }
- }
- }
- void crashdet_recover()
- {
- crashdet_restore_print_and_continue();
- if (lcd_crash_detect_enabled()) tmc2130_sg_stop_on_crash = true;
- }
- void crashdet_cancel()
- {
- saved_printing = false;
- tmc2130_sg_stop_on_crash = true;
- if (saved_printing_type == PRINTING_TYPE_SD) {
- lcd_print_stop();
- }else if(saved_printing_type == PRINTING_TYPE_USB){
- SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL);
- cmdqueue_reset();
- }
- }
- #endif
- void failstats_reset_print()
- {
- eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
- eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
- eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
- eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
- eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
- eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
- #if defined(FILAMENT_SENSOR) && defined(PAT9125)
- fsensor_softfail = 0;
- #endif
- }
- void softReset()
- {
- cli();
- wdt_enable(WDTO_15MS);
- while(1);
- }
- #ifdef MESH_BED_LEVELING
- enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet };
- #endif
- static void factory_reset_stats(){
- eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
- eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
- eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
- eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
- eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
- eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
- eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0);
- eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0);
- eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0);
- eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0);
- eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
- eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
- eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
- eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
- }
- static void factory_reset(char level)
- {
- lcd_clear();
- Sound_MakeCustom(100,0,false);
- switch (level) {
- case 0:
- lang_reset();
- break;
- case 1:
- factory_reset_stats();
- lcd_menu_statistics();
- break;
- case 2:
- factory_reset_stats();
-
- case 3:
-
- lang_reset();
-
- calibration_status_store(CALIBRATION_STATUS_Z_CALIBRATION);
- eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 2);
- farm_mode = false;
- eeprom_update_byte((uint8_t*)EEPROM_FARM_MODE, farm_mode);
- #ifdef FILAMENT_SENSOR
- fsensor_enable();
- fsensor_autoload_set(true);
- #endif
- break;
- case 4:
- menu_progressbar_init(EEPROM_TOP, PSTR("ERASING all data"));
-
- for (uint16_t i = 0; i < EEPROM_TOP; i++) {
- eeprom_update_byte((uint8_t*)i, 0xFF);
- menu_progressbar_update(i);
- }
- menu_progressbar_finish();
- softReset();
- break;
- default:
- break;
- }
- }
- extern "C" {
- FILE _uartout;
- }
- int uart_putchar(char c, FILE *)
- {
- MYSERIAL.write(c);
- return 0;
- }
- void lcd_splash()
- {
- lcd_clear();
- lcd_puts_P(PSTR("\n Original Prusa i3\n Prusa Research"));
- }
- void factory_reset()
- {
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- if (!READ(BTN_ENC))
- {
- _delay_ms(1000);
- if (!READ(BTN_ENC))
- {
- lcd_clear();
- lcd_puts_P(PSTR("Factory RESET"));
- SET_OUTPUT(BEEPER);
- if(eSoundMode!=e_SOUND_MODE_SILENT)
- WRITE(BEEPER, HIGH);
- while (!READ(BTN_ENC));
- WRITE(BEEPER, LOW);
- _delay_ms(2000);
- char level = reset_menu();
- factory_reset(level);
- switch (level) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4: _delay_ms(0); break;
- }
- }
- }
- KEEPALIVE_STATE(IN_HANDLER);
- }
- void show_fw_version_warnings() {
- if (FW_DEV_VERSION == FW_VERSION_GOLD || FW_DEV_VERSION == FW_VERSION_RC) return;
- switch (FW_DEV_VERSION) {
- case(FW_VERSION_ALPHA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware alpha version. This is development version. Using this version is not recommended and may cause printer damage.")); break;
- case(FW_VERSION_BETA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware beta version. This is development version. Using this version is not recommended and may cause printer damage.")); break;
- case(FW_VERSION_DEVEL):
- case(FW_VERSION_DEBUG):
- lcd_update_enable(false);
- lcd_clear();
- #if FW_DEV_VERSION == FW_VERSION_DEVEL
- lcd_puts_at_P(0, 0, PSTR("Development build !!"));
- #else
- lcd_puts_at_P(0, 0, PSTR("Debbugging build !!!"));
- #endif
- lcd_puts_at_P(0, 1, PSTR("May destroy printer!"));
- lcd_puts_at_P(0, 2, PSTR("ver ")); lcd_puts_P(PSTR(FW_VERSION_FULL));
- lcd_puts_at_P(0, 3, PSTR(FW_REPOSITORY));
- lcd_wait_for_click();
- break;
- }
- lcd_update_enable(true);
- }
- static void check_if_fw_is_on_right_printer(){
- #ifdef FILAMENT_SENSOR
- if((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3S)){
- #ifdef IR_SENSOR
- if (pat9125_probe()){
- lcd_show_fullscreen_message_and_wait_P(_i("MK3S firmware detected on MK3 printer"));}
- #endif
- #ifdef PAT9125
-
- const uint8_t ir_detected = !READ(IR_SENSOR_PIN);
- if (ir_detected){
- lcd_show_fullscreen_message_and_wait_P(_i("MK3 firmware detected on MK3S printer"));}
- #endif
- }
- #endif
- }
- uint8_t check_printer_version()
- {
- uint8_t version_changed = 0;
- uint16_t printer_type = eeprom_read_word((uint16_t*)EEPROM_PRINTER_TYPE);
- uint16_t motherboard = eeprom_read_word((uint16_t*)EEPROM_BOARD_TYPE);
- if (printer_type != PRINTER_TYPE) {
- if (printer_type == 0xffff) eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
- else version_changed |= 0b10;
- }
- if (motherboard != MOTHERBOARD) {
- if(motherboard == 0xffff) eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
- else version_changed |= 0b01;
- }
- return version_changed;
- }
- #ifdef BOOTAPP
- #include "bootapp.h"
- #endif
- #if (LANG_MODE != 0)
- #ifdef XFLASH
- #define LANGBOOT_BLOCKSIZE 0x1000u
- #define LANGBOOT_RAMBUFFER 0x0800
- void update_sec_lang_from_external_flash()
- {
- if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0))
- {
- uint8_t lang = boot_reserved >> 3;
- uint8_t state = boot_reserved & 0x07;
- lang_table_header_t header;
- uint32_t src_addr;
- if (lang_get_header(lang, &header, &src_addr))
- {
- lcd_puts_at_P(1,3,PSTR("Language update."));
- for (uint8_t i = 0; i < state; i++) fputc('.', lcdout);
- _delay(100);
- boot_reserved = (boot_reserved & 0xF8) | ((state + 1) & 0x07);
- if ((state * LANGBOOT_BLOCKSIZE) < header.size)
- {
- cli();
- uint16_t size = header.size - state * LANGBOOT_BLOCKSIZE;
- if (size > LANGBOOT_BLOCKSIZE) size = LANGBOOT_BLOCKSIZE;
- xflash_rd_data(src_addr + state * LANGBOOT_BLOCKSIZE, (uint8_t*)LANGBOOT_RAMBUFFER, size);
- if (state == 0)
- {
-
- }
- bootapp_ram2flash(LANGBOOT_RAMBUFFER, _SEC_LANG_TABLE + state * LANGBOOT_BLOCKSIZE, size);
- }
- else
- {
-
- eeprom_update_byte((unsigned char *)EEPROM_LANG, LANG_ID_SEC);
- }
- }
- }
- boot_app_flags &= ~BOOT_APP_FLG_USER0;
- }
- #ifdef DEBUG_XFLASH
- uint8_t lang_xflash_enum_codes(uint16_t* codes)
- {
- lang_table_header_t header;
- uint8_t count = 0;
- uint32_t addr = 0x00000;
- while (1)
- {
- printf_P(_n("LANGTABLE%d:"), count);
- xflash_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t));
- if (header.magic != LANG_MAGIC)
- {
- puts_P(_n("NG!"));
- break;
- }
- puts_P(_n("OK"));
- printf_P(_n(" _lt_magic = 0x%08lx %S\n"), header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA"));
- printf_P(_n(" _lt_size = 0x%04x (%d)\n"), header.size, header.size);
- printf_P(_n(" _lt_count = 0x%04x (%d)\n"), header.count, header.count);
- printf_P(_n(" _lt_chsum = 0x%04x\n"), header.checksum);
- printf_P(_n(" _lt_code = 0x%04x (%c%c)\n"), header.code, header.code >> 8, header.code & 0xff);
- printf_P(_n(" _lt_sign = 0x%08lx\n"), header.signature);
- addr += header.size;
- codes[count] = header.code;
- count ++;
- }
- return count;
- }
- void list_sec_lang_from_external_flash()
- {
- uint16_t codes[8];
- uint8_t count = lang_xflash_enum_codes(codes);
- printf_P(_n("XFlash lang count = %hhd\n"), count);
- }
- #endif
- #endif
- #endif
- static void fw_crash_init()
- {
- #ifdef XFLASH_DUMP
- dump_crash_reason crash_reason;
- if(xfdump_check_state(&crash_reason))
- {
-
- puts_P(_N("// action:dump_available"));
- #ifdef EMERGENCY_DUMP
- if(crash_reason != dump_crash_reason::manual &&
- eeprom_read_byte((uint8_t*)EEPROM_FW_CRASH_FLAG) != 0xFF)
- {
- lcd_show_fullscreen_message_and_wait_P(
- _n("FW crash detected! "
- "You can continue printing. "
- "Debug data available for analysis. "
- "Contact support to submit details."));
- }
- #endif
- }
- #else
- dump_crash_reason crash_reason = (dump_crash_reason)eeprom_read_byte((uint8_t*)EEPROM_FW_CRASH_FLAG);
- if(crash_reason != dump_crash_reason::manual && (uint8_t)crash_reason != 0xFF)
- {
- lcd_beeper_quick_feedback();
- lcd_clear();
- lcd_puts_P(_n("FIRMWARE CRASH!\nCrash reason:\n"));
- switch(crash_reason)
- {
- case dump_crash_reason::stack_error:
- lcd_puts_P(_n("Static memory has\nbeen overwritten"));
- break;
- case dump_crash_reason::watchdog:
- lcd_puts_P(_n("Watchdog timeout"));
- break;
- case dump_crash_reason::bad_isr:
- lcd_puts_P(_n("Bad interrupt"));
- break;
- default:
- lcd_print((uint8_t)crash_reason);
- break;
- }
- lcd_wait_for_click();
- }
- #endif
-
- eeprom_update_byte((uint8_t*)EEPROM_FW_CRASH_FLAG, 0xFF);
- }
- static void xflash_err_msg()
- {
- puts_P(_n("XFLASH not responding."));
- lcd_show_fullscreen_message_and_wait_P(_n("External SPI flash\nXFLASH is not res-\nponding. Language\nswitch unavailable."));
- }
- void setup()
- {
- timer2_init();
- mmu_init();
- ultralcd_init();
- spi_init();
- lcd_splash();
- Sound_Init();
- selectedSerialPort = eeprom_read_byte((uint8_t *)EEPROM_SECOND_SERIAL_ACTIVE);
- if (selectedSerialPort == 0xFF) selectedSerialPort = 0;
- eeprom_update_byte((uint8_t *)EEPROM_SECOND_SERIAL_ACTIVE, selectedSerialPort);
- MYSERIAL.begin(BAUDRATE);
- fdev_setup_stream(uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
- stdout = uartout;
- #ifdef XFLASH
- bool xflash_success = xflash_init();
- uint8_t optiboot_status = 1;
- if (xflash_success)
- {
- optiboot_status = optiboot_xflash_enter();
- #if (LANG_MODE != 0)
- update_sec_lang_from_external_flash();
- #endif
- }
- #else
- const bool xflash_success = true;
- #endif
- setup_killpin();
- setup_powerhold();
- farm_mode = eeprom_read_byte((uint8_t*)EEPROM_FARM_MODE);
- if (farm_mode == 0xFF) {
- farm_mode = false;
- eeprom_update_byte((uint8_t*)EEPROM_FARM_MODE, farm_mode);
- } else if (farm_mode) {
- no_response = true;
- important_status = 8;
- prusa_statistics(8);
- #ifdef HAS_SECOND_SERIAL_PORT
- selectedSerialPort = 1;
- #endif
- MYSERIAL.begin(BAUDRATE);
- #ifdef FILAMENT_SENSOR
-
- fsensor_autoload_set(false);
- #endif
-
- eeprom_update_byte((uint8_t*)EEPROM_FAN_CHECK_ENABLED, true);
- }
- #ifdef TMC2130
- if( FarmOrUserECool() ){
-
- tmc2130_current_h[E_AXIS] = TMC2130_CURRENTS_FARM;
- tmc2130_current_r[E_AXIS] = TMC2130_CURRENTS_FARM;
- }
- #endif
- #ifdef PRUSA_SN_SUPPORT
-
-
- {
- char SN[20];
- eeprom_read_block(SN, (uint8_t*)EEPROM_PRUSA_SN, 20);
- if (SN[19] || strncmp_P(SN, PSTR("CZPX"), 4))
- {
- if (!get_PRUSA_SN(SN))
- {
- eeprom_update_block(SN, (uint8_t*)EEPROM_PRUSA_SN, 20);
- puts_P(PSTR("SN updated"));
- }
- else
- puts_P(PSTR("SN update failed"));
- }
- }
- #endif
- #ifndef XFLASH
- SERIAL_PROTOCOLLNPGM("start");
- #else
- if ((optiboot_status != 0) || (selectedSerialPort != 0))
- SERIAL_PROTOCOLLNPGM("start");
- #endif
- SERIAL_ECHO_START;
- puts_P(PSTR(" " FW_VERSION_FULL));
-
- #ifdef DEBUG_SEC_LANG
- lang_table_header_t header;
- uint32_t src_addr = 0x00000;
- if (lang_get_header(1, &header, &src_addr))
- {
- printf_P(
- _n(
- " _src_addr = 0x%08lx\n"
- " _lt_magic = 0x%08lx %S\n"
- " _lt_size = 0x%04x (%d)\n"
- " _lt_count = 0x%04x (%d)\n"
- " _lt_chsum = 0x%04x\n"
- " _lt_code = 0x%04x (%c%c)\n"
- " _lt_resv1 = 0x%08lx\n"
- ),
- src_addr,
- header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA"),
- header.size, header.size,
- header.count, header.count,
- header.checksum,
- header.code, header.code >> 8, header.code & 0xff,
- header.signature
- );
- #if 0
- xflash_rd_data(0x25ba, (uint8_t*)&block_buffer, 1024);
- for (uint16_t i = 0; i < 1024; i++)
- {
- if ((i % 16) == 0) printf_P(_n("%04x:"), 0x25ba+i);
- printf_P(_n(" %02x"), ((uint8_t*)&block_buffer)[i]);
- if ((i % 16) == 15) putchar('\n');
- }
- #endif
- uint16_t sum = 0;
- for (uint16_t i = 0; i < header.size; i++)
- sum += (uint16_t)pgm_read_byte((uint8_t*)(_SEC_LANG_TABLE + i)) << ((i & 1)?0:8);
- printf_P(_n("_SEC_LANG_TABLE checksum = %04x\n"), sum);
- sum -= header.checksum;
- printf_P(_n("_SEC_LANG_TABLE checksum = %04x\n"), sum);
- sum = (sum >> 8) | ((sum & 0xff) << 8);
- if (sum == header.checksum)
- puts_P(_n("Checksum OK"));
- else
- puts_P(_n("Checksum NG"));
- }
- else
- puts_P(_n("lang_get_header failed!"));
- #if 0
- for (uint16_t i = 0; i < 1024*10; i++)
- {
- if ((i % 16) == 0) printf_P(_n("%04x:"), _SEC_LANG_TABLE+i);
- printf_P(_n(" %02x"), pgm_read_byte((uint8_t*)(_SEC_LANG_TABLE+i)));
- if ((i % 16) == 15) putchar('\n');
- }
- #endif
- #if 0
- SERIAL_ECHOLN("Reading eeprom from 0 to 100: start");
- for (int i = 0; i < 4096; ++i) {
- int b = eeprom_read_byte((unsigned char*)i);
- if (b != 255) {
- SERIAL_ECHO(i);
- SERIAL_ECHO(":");
- SERIAL_ECHO(b);
- SERIAL_ECHOLN("");
- }
- }
- SERIAL_ECHOLN("Reading eeprom from 0 to 100: done");
- #endif
- #endif
-
- byte mcu = MCUSR;
- if (mcu & 1) puts_P(MSG_POWERUP);
- if (mcu & 2) puts_P(MSG_EXTERNAL_RESET);
- if (mcu & 4) puts_P(MSG_BROWNOUT_RESET);
- if (mcu & 8) puts_P(MSG_WATCHDOG_RESET);
- if (mcu & 32) puts_P(MSG_SOFTWARE_RESET);
- MCUSR = 0;
-
-
- #ifdef STRING_VERSION_CONFIG_H
- #ifdef STRING_CONFIG_H_AUTHOR
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(_n(" Last Updated: "));
- SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
- SERIAL_ECHORPGM(_n(" | Author: "));
- SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
- SERIAL_ECHOPGM("Compiled: ");
- SERIAL_ECHOLNPGM(__DATE__);
- #endif
- #endif
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(_n(" Free Memory: "));
- SERIAL_ECHO(freeMemory());
- SERIAL_ECHORPGM(_n(" PlannerBufferBytes: "));
- SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
-
-
-
- bool previous_settings_retrieved = false;
- uint8_t hw_changed = check_printer_version();
- if (!(hw_changed & 0b10)) {
- previous_settings_retrieved = Config_RetrieveSettings();
- }
- else {
- Config_ResetDefault();
- }
- SdFatUtil::set_stack_guard();
- tp_init();
- #ifdef EXTRUDER_ALTFAN_DETECT
- SERIAL_ECHORPGM(_n("Extruder fan type: "));
- if (extruder_altfan_detect())
- SERIAL_ECHOLNRPGM(PSTR("ALTFAN"));
- else
- SERIAL_ECHOLNRPGM(PSTR("NOCTUA"));
- #endif
- plan_init();
- factory_reset();
- if (eeprom_read_dword((uint32_t*)(EEPROM_TOP - 4)) == 0x0ffffffff &&
- eeprom_read_dword((uint32_t*)(EEPROM_TOP - 8)) == 0x0ffffffff)
- {
-
-
-
-
-
- eeprom_update_byte((uint8_t*)EEPROM_SILENT, SILENT_MODE_OFF);
- #ifdef TMC2130
- tmc2130_mode = TMC2130_MODE_NORMAL;
- #endif
- eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1);
- }
- lcd_encoder_diff=0;
- #ifdef TMC2130
- uint8_t silentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
- if (silentMode == 0xff) silentMode = 0;
- tmc2130_mode = TMC2130_MODE_NORMAL;
- if (lcd_crash_detect_enabled() && !farm_mode)
- {
- lcd_crash_detect_enable();
- puts_P(_N("CrashDetect ENABLED!"));
- }
- else
- {
- lcd_crash_detect_disable();
- puts_P(_N("CrashDetect DISABLED"));
- }
- #ifdef TMC2130_LINEARITY_CORRECTION
- #ifdef TMC2130_LINEARITY_CORRECTION_XYZ
- tmc2130_wave_fac[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_X_FAC);
- tmc2130_wave_fac[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Y_FAC);
- tmc2130_wave_fac[Z_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Z_FAC);
- #endif
- tmc2130_wave_fac[E_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_E_FAC);
- if (tmc2130_wave_fac[X_AXIS] == 0xff) tmc2130_wave_fac[X_AXIS] = 0;
- if (tmc2130_wave_fac[Y_AXIS] == 0xff) tmc2130_wave_fac[Y_AXIS] = 0;
- if (tmc2130_wave_fac[Z_AXIS] == 0xff) tmc2130_wave_fac[Z_AXIS] = 0;
- if (tmc2130_wave_fac[E_AXIS] == 0xff) tmc2130_wave_fac[E_AXIS] = 0;
- #endif
- #ifdef TMC2130_VARIABLE_RESOLUTION
- tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[X_AXIS]);
- tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[Y_AXIS]);
- tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[Z_AXIS]);
- tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[E_AXIS]);
- #else
- tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
- tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY);
- tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_Z);
- tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_E);
- #endif
- #endif
- st_init();
-
- #ifdef TMC2130
- tmc2130_mode = silentMode?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL;
- update_mode_profile();
- tmc2130_init(TMCInitParams(false, FarmOrUserECool() ));
- #endif
- #ifdef PSU_Delta
- init_force_z();
- #endif
-
- setup_photpin();
- servo_init();
-
-
- world2machine_reset();
-
-
- clamp_to_software_endstops(current_position);
- plan_set_position_curposXYZE();
-
- if (!xflash_success)
- xflash_err_msg();
- #ifdef FILAMENT_SENSOR
- fsensor_init();
- #endif
- #if defined(CONTROLLERFAN_PIN) && (CONTROLLERFAN_PIN > -1)
- SET_OUTPUT(CONTROLLERFAN_PIN);
- #endif
- setup_homepin();
- #if defined(Z_AXIS_ALWAYS_ON)
- enable_z();
- #endif
- if (farm_mode) {
-
-
- prusa_statistics(8);
- }
-
- card.ToshibaFlashAir_enable(eeprom_read_byte((unsigned char*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY) == 1);
-
-
- card.initsd();
- #ifdef DEBUG_SD_SPEED_TEST
- if (card.cardOK)
- {
- uint8_t* buff = (uint8_t*)block_buffer;
- uint32_t block = 0;
- uint32_t sumr = 0;
- uint32_t sumw = 0;
- for (int i = 0; i < 1024; i++)
- {
- uint32_t u = _micros();
- bool res = card.card.readBlock(i, buff);
- u = _micros() - u;
- if (res)
- {
- printf_P(PSTR("readBlock %4d 512 bytes %lu us\n"), i, u);
- sumr += u;
- u = _micros();
- res = card.card.writeBlock(i, buff);
- u = _micros() - u;
- if (res)
- {
- printf_P(PSTR("writeBlock %4d 512 bytes %lu us\n"), i, u);
- sumw += u;
- }
- else
- {
- printf_P(PSTR("writeBlock %4d error\n"), i);
- break;
- }
- }
- else
- {
- printf_P(PSTR("readBlock %4d error\n"), i);
- break;
- }
- }
- uint32_t avg_rspeed = (1024 * 1000000) / (sumr / 512);
- uint32_t avg_wspeed = (1024 * 1000000) / (sumw / 512);
- printf_P(PSTR("avg read speed %lu bytes/s\n"), avg_rspeed);
- printf_P(PSTR("avg write speed %lu bytes/s\n"), avg_wspeed);
- }
- else
- printf_P(PSTR("Card NG!\n"));
- #endif
- eeprom_init();
-
-
-
- #if (LANG_MODE != 0)
- #ifdef DEBUG_XFLASH
- XFLASH_SPI_ENTER();
- uint8_t uid[8];
- xflash_rd_uid(uid);
- puts_P(_n("XFLASH UID="));
- for (uint8_t i = 0; i < 8; i ++)
- printf_P(PSTR("%02x"), uid[i]);
- putchar('\n');
- list_sec_lang_from_external_flash();
- #endif
- if (!lang_select(eeprom_read_byte((uint8_t*)EEPROM_LANG)))
- lcd_language();
- #ifdef DEBUG_SEC_LANG
- uint16_t sec_lang_code = lang_get_code(1);
- uint16_t ui = _SEC_LANG_TABLE;
- printf_P(_n("lang_selected=%d\nlang_table=0x%04x\nSEC_LANG_CODE=0x%04x (%c%c)\n"), lang_selected, ui, sec_lang_code, sec_lang_code >> 8, sec_lang_code & 0xff);
- lang_print_sec_lang(uartout);
- #endif
- #endif
- if (eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE) == 255) {
- eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
- }
- if (eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA) == 255) {
-
- eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
- int16_t z_shift = 0;
- for (uint8_t i = 0; i < 5; i++) {
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
- }
- eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
- }
- if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 255) {
- eeprom_write_byte((uint8_t*)EEPROM_UVLO, 0);
- }
- if (eeprom_read_byte((uint8_t*)EEPROM_SD_SORT) == 255) {
- eeprom_write_byte((uint8_t*)EEPROM_SD_SORT, 0);
- }
-
- mbl_settings_init();
- SilentModeMenu_MMU = eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH);
- if (SilentModeMenu_MMU == 255) {
- SilentModeMenu_MMU = 1;
- eeprom_write_byte((uint8_t*)EEPROM_MMU_STEALTH, SilentModeMenu_MMU);
- }
- #if !defined(DEBUG_DISABLE_FANCHECK) && defined(FANCHECK) && defined(TACH_1) && TACH_1 >-1
- setup_fan_interrupt();
- #endif
- #ifdef PAT9125
- fsensor_setup_interrupt();
- #endif
- #ifndef DEBUG_DISABLE_STARTMSGS
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- if (!farm_mode) {
- check_if_fw_is_on_right_printer();
- show_fw_version_warnings();
- }
- switch (hw_changed) {
-
-
- case(0b01):
- lcd_show_fullscreen_message_and_wait_P(_i("Warning: motherboard type changed."));
- eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
- break;
- case(0b10):
- lcd_show_fullscreen_message_and_wait_P(_i("Warning: printer type changed."));
- eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
- break;
- case(0b11):
- lcd_show_fullscreen_message_and_wait_P(_i("Warning: both printer type and motherboard type changed."));
- eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
- eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
- break;
- default: break;
- }
- if (!previous_settings_retrieved) {
- lcd_show_fullscreen_message_and_wait_P(_i("Old settings found. Default PID, Esteps etc. will be set."));
- Config_StoreSettings();
- }
- if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) >= 1) {
- lcd_wizard(WizState::Run);
- }
- if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 0) {
- if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED ||
- calibration_status() == CALIBRATION_STATUS_UNKNOWN ||
- calibration_status() == CALIBRATION_STATUS_XYZ_CALIBRATION) {
-
- eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0);
-
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_CALIBRATION_FLOW));
- }
- else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) {
-
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET));
- lcd_update_enable(true);
- }
- else if (calibration_status() == CALIBRATION_STATUS_CALIBRATED && eeprom_read_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE) && calibration_status_pinda() == false) {
-
- lcd_update_enable(true);
- }
- else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) {
-
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_Z_CALIBRATION_FLOW));
- }
- }
- #if !defined (DEBUG_DISABLE_FORCE_SELFTEST) && defined (TMC2130)
- if (force_selftest_if_fw_version() && calibration_status() < CALIBRATION_STATUS_ASSEMBLED) {
- lcd_show_fullscreen_message_and_wait_P(_i("Selftest will be run to calibrate accurate sensorless rehoming."));
- update_current_firmware_version_to_eeprom();
- lcd_selftest();
- }
- #endif
- KEEPALIVE_STATE(IN_PROCESS);
- #endif
- lcd_update_enable(true);
- lcd_clear();
- lcd_update(2);
-
-
- update_current_firmware_version_to_eeprom();
- #ifdef TMC2130
- tmc2130_home_origin[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN);
- tmc2130_home_bsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_BSTEPS);
- tmc2130_home_fsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_FSTEPS);
- if (tmc2130_home_origin[X_AXIS] == 0xff) tmc2130_home_origin[X_AXIS] = 0;
- if (tmc2130_home_bsteps[X_AXIS] == 0xff) tmc2130_home_bsteps[X_AXIS] = 48;
- if (tmc2130_home_fsteps[X_AXIS] == 0xff) tmc2130_home_fsteps[X_AXIS] = 48;
- tmc2130_home_origin[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN);
- tmc2130_home_bsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_BSTEPS);
- tmc2130_home_fsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_FSTEPS);
- if (tmc2130_home_origin[Y_AXIS] == 0xff) tmc2130_home_origin[Y_AXIS] = 0;
- if (tmc2130_home_bsteps[Y_AXIS] == 0xff) tmc2130_home_bsteps[Y_AXIS] = 48;
- if (tmc2130_home_fsteps[Y_AXIS] == 0xff) tmc2130_home_fsteps[Y_AXIS] = 48;
- tmc2130_home_enabled = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_ENABLED);
- if (tmc2130_home_enabled == 0xff) tmc2130_home_enabled = 0;
- #endif
-
- fw_crash_init();
- #ifdef UVLO_SUPPORT
- if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != 0) {
- manage_heater();
- #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
- printf_P(_N("Power panic detected!\nCurrent bed temp:%d\nSaved bed temp:%d\n"), (int)degBed(), eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED));
- #endif
- if ( degBed() > ( (float)eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED) - AUTOMATIC_UVLO_BED_TEMP_OFFSET) ){
- #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
- puts_P(_N("Automatic recovery!"));
- #endif
- recover_print(1);
- }
- else{
- #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER
- puts_P(_N("Normal recovery!"));
- #endif
- if ( lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false) ) recover_print(0);
- else {
- eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0);
- lcd_update_enable(true);
- lcd_update(2);
- lcd_setstatuspgm(MSG_WELCOME);
- }
- }
- }
-
-
- setup_uvlo_interrupt();
- #endif
- fCheckModeInit();
- fSetMmuMode(mmu_enabled);
- KEEPALIVE_STATE(NOT_BUSY);
- #ifdef WATCHDOG
- wdt_enable(WDTO_4S);
- #ifdef EMERGENCY_HANDLERS
- WDTCSR |= (1 << WDIE);
- #endif
- #endif
- }
- static inline void crash_and_burn(dump_crash_reason reason)
- {
- WRITE(BEEPER, HIGH);
- eeprom_update_byte((uint8_t*)EEPROM_FW_CRASH_FLAG, (uint8_t)reason);
- #ifdef EMERGENCY_DUMP
- xfdump_full_dump_and_reset(reason);
- #elif defined(EMERGENCY_SERIAL_DUMP)
- if(emergency_serial_dump)
- serial_dump_and_reset(reason);
- #endif
- softReset();
- }
- #ifdef EMERGENCY_HANDLERS
- #ifdef WATCHDOG
- ISR(WDT_vect)
- {
- crash_and_burn(dump_crash_reason::watchdog);
- }
- #endif
- ISR(BADISR_vect)
- {
- crash_and_burn(dump_crash_reason::bad_isr);
- }
- #endif
- void stack_error() {
- crash_and_burn(dump_crash_reason::stack_error);
- }
- void pullup_error(bool fromTempISR) {
- crash_and_burn(fromTempISR ? dump_crash_reason::bad_pullup_temp_isr : dump_crash_reason::bad_pullup_step_isr);
- }
- #ifdef PRUSA_M28
- void trace();
- #define CHUNK_SIZE 64
- #define SAFETY_MARGIN 1
- char chunk[CHUNK_SIZE+SAFETY_MARGIN];
- void serial_read_stream() {
- setAllTargetHotends(0);
- setTargetBed(0);
- lcd_clear();
- lcd_puts_P(PSTR(" Upload in progress"));
-
- uint32_t bytesToReceive;
-
- char bytesToReceiveBuffer[4];
- for (int i=0; i<4; i++) {
- int data;
- while ((data = MYSERIAL.read()) == -1) {};
- bytesToReceiveBuffer[i] = data;
- }
-
- memcpy(&bytesToReceive, &bytesToReceiveBuffer, 4);
-
- MYSERIAL.write('+');
-
- uint32_t receivedBytes = 0;
- while (prusa_sd_card_upload) {
- int i;
- for (i=0; i<CHUNK_SIZE; i++) {
- int data;
-
- if (receivedBytes == bytesToReceive) {
- break;
- }
-
- while ((data = MYSERIAL.read()) == -1) {};
- receivedBytes++;
-
- chunk[i] = data;
- }
-
- card.write_command_no_newline(&chunk[0]);
-
- MYSERIAL.write('+');
-
- manage_heater();
-
- if(receivedBytes == bytesToReceive) {
- trace();
- card.closefile();
- prusa_sd_card_upload = false;
- SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
- }
- }
- }
- #endif
- #if defined(AUTO_REPORT)
- static void host_autoreport()
- {
- if (autoReportFeatures.TimerExpired())
- {
- if(autoReportFeatures.Temp()){
- gcode_M105(active_extruder);
- }
- if(autoReportFeatures.Pos()){
- gcode_M114();
- }
- #if defined(AUTO_REPORT) && (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
- if(autoReportFeatures.Fans()){
- gcode_M123();
- }
- #endif
- autoReportFeatures.TimerStart();
- }
- }
- #endif
- void host_keepalive() {
- #ifndef HOST_KEEPALIVE_FEATURE
- return;
- #endif
- if (farm_mode) return;
- long ms = _millis();
- if (host_keepalive_interval && busy_state != NOT_BUSY) {
- if ((ms - prev_busy_signal_ms) < (long)(1000L * host_keepalive_interval)) return;
- switch (busy_state) {
- case IN_HANDLER:
- case IN_PROCESS:
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM("busy: processing");
- break;
- case PAUSED_FOR_USER:
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM("busy: paused for user");
- break;
- case PAUSED_FOR_INPUT:
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM("busy: paused for input");
- break;
- default:
- break;
- }
- }
- prev_busy_signal_ms = ms;
- }
- void loop()
- {
- KEEPALIVE_STATE(NOT_BUSY);
- if (isPrintPaused && saved_printing_type == PRINTING_TYPE_USB) {
- usb_timer.start();
- }
- else if (usb_timer.expired(10000)) {
- ;
- }
-
- #ifdef FANCHECK
- if (fan_check_error && isPrintPaused && !IS_SD_PRINTING) {
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- host_keepalive();
- }
- #endif
- #ifdef PRUSA_M28
- if (prusa_sd_card_upload)
- {
-
- serial_read_stream();
- }
- else
- #endif
- {
- get_command();
- #ifdef SDSUPPORT
- card.checkautostart(false);
- #endif
- if(buflen)
- {
- cmdbuffer_front_already_processed = false;
- #ifdef SDSUPPORT
- if(card.saving)
- {
-
-
- if(strstr_P(CMDBUFFER_CURRENT_STRING, PSTR("M29")) == NULL) {
- card.write_command(CMDBUFFER_CURRENT_STRING);
- if(card.logging)
- process_commands();
- else
- SERIAL_PROTOCOLLNRPGM(MSG_OK);
- } else {
- card.closefile();
- SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED);
- }
- } else {
- process_commands();
- }
- #else
- process_commands();
- #endif
- if (! cmdbuffer_front_already_processed && buflen)
- {
-
-
- char *ptr = cmdbuffer + bufindr;
- if (*ptr == CMDBUFFER_CURRENT_TYPE_SDCARD) {
-
- union {
- struct {
- char lo;
- char hi;
- } lohi;
- uint16_t value;
- } sdlen;
- sdlen.value = 0;
- {
-
-
-
-
- cli();
-
-
- *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED;
-
- sdlen.lohi.lo = *ptr ++;
- sdlen.lohi.hi = *ptr;
-
- planner_add_sd_length(sdlen.value);
- sei();
- }
- }
- else if((*ptr == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR) && !IS_SD_PRINTING){
-
- cli();
- *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED;
-
- planner_add_sd_length(1);
- sei();
- }
-
-
-
- cmdqueue_pop_front();
- }
- host_keepalive();
- }
- }
-
- manage_heater();
- isPrintPaused ? manage_inactivity(true) : manage_inactivity(false);
- checkHitEndstops();
- lcd_update(0);
- #ifdef TMC2130
- tmc2130_check_overtemp();
- if (tmc2130_sg_crash)
- {
- uint8_t crash = tmc2130_sg_crash;
- tmc2130_sg_crash = 0;
- switch (crash)
- {
- case 1: enquecommand_P((PSTR("CRASH_DETECTEDX"))); break;
- case 2: enquecommand_P((PSTR("CRASH_DETECTEDY"))); break;
- case 3: enquecommand_P((PSTR("CRASH_DETECTEDXY"))); break;
- }
- }
- #endif
- mmu_loop();
- }
- #define DEFINE_PGM_READ_ANY(type, reader) \
- static inline type pgm_read_any(const type *p) \
- { return pgm_read_##reader##_near(p); }
- DEFINE_PGM_READ_ANY(float, float);
- DEFINE_PGM_READ_ANY(signed char, byte);
- #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
- static const PROGMEM type array##_P[3] = \
- { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
- static inline type array(uint8_t axis) \
- { return pgm_read_any(&array##_P[axis]); } \
- type array##_ext(uint8_t axis) \
- { return pgm_read_any(&array##_P[axis]); }
- XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
- XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
- XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
- XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
- XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
- XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
- static void axis_is_at_home(uint8_t axis) {
- current_position[axis] = base_home_pos(axis) + cs.add_homing[axis];
- min_pos[axis] = base_min_pos(axis) + cs.add_homing[axis];
- max_pos[axis] = base_max_pos(axis) + cs.add_homing[axis];
- }
- static int setup_for_endstop_move(bool enable_endstops_now = true) {
- saved_feedrate = feedrate;
- int l_feedmultiply = feedmultiply;
- feedmultiply = 100;
- previous_millis_cmd.start();
-
- enable_endstops(enable_endstops_now);
- return l_feedmultiply;
- }
- static void clean_up_after_endstop_move(int original_feedmultiply) {
- #ifdef ENDSTOPS_ONLY_FOR_HOMING
- enable_endstops(false);
- #endif
-
- feedrate = saved_feedrate;
- feedmultiply = original_feedmultiply;
- previous_millis_cmd.start();
- }
- #ifdef ENABLE_AUTO_BED_LEVELING
- #ifdef AUTO_BED_LEVELING_GRID
- static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
- {
- vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
- planeNormal.debug("planeNormal");
- plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
-
-
-
-
- vector_3 corrected_position = plan_get_position();
- current_position[X_AXIS] = corrected_position.x;
- current_position[Y_AXIS] = corrected_position.y;
- current_position[Z_AXIS] = corrected_position.z;
-
- current_position[Z_AXIS] = cs.zprobe_zoffset;
- plan_set_position_curposXYZE();
- }
- #else
- static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
- plan_bed_level_matrix.set_to_identity();
- vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
- vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
- vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
- vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
- vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
- vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
- planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
- plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
- vector_3 corrected_position = plan_get_position();
- current_position[X_AXIS] = corrected_position.x;
- current_position[Y_AXIS] = corrected_position.y;
- current_position[Z_AXIS] = corrected_position.z;
-
- current_position[Z_AXIS] = cs.zprobe_zoffset;
- plan_set_position_curposXYZE();
- }
- #endif
- static void run_z_probe() {
- plan_bed_level_matrix.set_to_identity();
- feedrate = homing_feedrate[Z_AXIS];
-
- float zPosition = -10;
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
- st_synchronize();
-
- zPosition = st_get_position_mm(Z_AXIS);
- plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
-
- zPosition += home_retract_mm(Z_AXIS);
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
- st_synchronize();
-
- feedrate = homing_feedrate[Z_AXIS]/4;
- zPosition -= home_retract_mm(Z_AXIS) * 2;
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
- st_synchronize();
- current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
-
- plan_set_position_curposXYZE();
- }
- static void do_blocking_move_to(float x, float y, float z) {
- float oldFeedRate = feedrate;
- feedrate = homing_feedrate[Z_AXIS];
- current_position[Z_AXIS] = z;
- plan_buffer_line_curposXYZE(feedrate/60, active_extruder);
- st_synchronize();
- feedrate = XY_TRAVEL_SPEED;
- current_position[X_AXIS] = x;
- current_position[Y_AXIS] = y;
- plan_buffer_line_curposXYZE(feedrate/60, active_extruder);
- st_synchronize();
- feedrate = oldFeedRate;
- }
- static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
- do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
- }
- static float probe_pt(float x, float y, float z_before) {
-
- do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
- do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
- run_z_probe();
- float measured_z = current_position[Z_AXIS];
- SERIAL_PROTOCOLRPGM(_T(MSG_BED));
- SERIAL_PROTOCOLPGM(" x: ");
- SERIAL_PROTOCOL(x);
- SERIAL_PROTOCOLPGM(" y: ");
- SERIAL_PROTOCOL(y);
- SERIAL_PROTOCOLPGM(" z: ");
- SERIAL_PROTOCOL(measured_z);
- SERIAL_PROTOCOLPGM("\n");
- return measured_z;
- }
- #endif
- #ifdef LIN_ADVANCE
-
- inline void gcode_M900() {
- float newK = code_seen('K') ? code_value_float() : -2;
- #ifdef LA_NOCOMPAT
- if (newK >= 0 && newK < LA_K_MAX)
- extruder_advance_K = newK;
- else
- SERIAL_ECHOLNPGM("K out of allowed range!");
- #else
- if (newK == 0)
- {
- extruder_advance_K = 0;
- la10c_reset();
- }
- else
- {
- newK = la10c_value(newK);
- if (newK < 0)
- SERIAL_ECHOLNPGM("K out of allowed range!");
- else
- extruder_advance_K = newK;
- }
- #endif
- SERIAL_ECHO_START;
- SERIAL_ECHOPGM("Advance K=");
- SERIAL_ECHOLN(extruder_advance_K);
- }
- #endif
- bool check_commands() {
- bool end_command_found = false;
-
- while (buflen)
- {
- if ((code_seen_P(PSTR("M84"))) || (code_seen_P(PSTR("M 84")))) end_command_found = true;
- if (!cmdbuffer_front_already_processed)
- cmdqueue_pop_front();
- cmdbuffer_front_already_processed = false;
- }
- return end_command_found;
-
- }
- void raise_z_above(float target, bool plan)
- {
- if (current_position[Z_AXIS] >= target)
- return;
-
- current_position[Z_AXIS] = target;
- #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
- bool z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
- #else
- bool z_min_endstop = false;
- #endif
- if (axis_known_position[Z_AXIS] || z_min_endstop)
- {
-
- if(plan) plan_buffer_line_curposXYZE(max_feedrate[Z_AXIS]);
- return;
- }
-
- enable_z();
- st_synchronize();
-
- bool z_endstop_enabled = enable_z_endstop(true);
- #ifdef TMC2130
- tmc2130_home_enter(Z_AXIS_MASK);
- #endif
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 60);
- st_synchronize();
- #ifdef TMC2130
- if (endstop_z_hit_on_purpose())
- {
-
- current_position[Z_AXIS] = max_pos[Z_AXIS];
- plan_set_position_curposXYZE();
- }
- tmc2130_home_exit();
- #endif
- enable_z_endstop(z_endstop_enabled);
- }
- #ifdef TMC2130
- bool calibrate_z_auto()
- {
-
- lcd_clear();
- lcd_puts_at_P(0, 1, _T(MSG_CALIBRATE_Z_AUTO));
- bool endstops_enabled = enable_endstops(true);
- int axis_up_dir = -home_dir(Z_AXIS);
- tmc2130_home_enter(Z_AXIS_MASK);
- current_position[Z_AXIS] = 0;
- plan_set_position_curposXYZE();
- set_destination_to_current();
- destination[Z_AXIS] += (1.1 * max_length(Z_AXIS) * axis_up_dir);
- feedrate = homing_feedrate[Z_AXIS];
- plan_buffer_line_destinationXYZE(feedrate / 60);
- st_synchronize();
-
-
- tmc2130_home_exit();
- enable_endstops(false);
- current_position[Z_AXIS] = 0;
- plan_set_position_curposXYZE();
- set_destination_to_current();
- destination[Z_AXIS] += 10 * axis_up_dir;
- feedrate = homing_feedrate[Z_AXIS] / 2;
- plan_buffer_line_destinationXYZE(feedrate / 60);
- st_synchronize();
- enable_endstops(endstops_enabled);
- current_position[Z_AXIS] = Z_MAX_POS + 3.0;
- plan_set_position_curposXYZE();
- return true;
- }
- #endif
- #ifdef TMC2130
- static void check_Z_crash(void)
- {
- if (READ(Z_TMC2130_DIAG) != 0) {
- FORCE_HIGH_POWER_END;
- current_position[Z_AXIS] = 0;
- plan_set_position_curposXYZE();
- current_position[Z_AXIS] += MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(max_feedrate[Z_AXIS]);
- st_synchronize();
- kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
- }
- }
- #endif
- #ifdef TMC2130
- void homeaxis(uint8_t axis, uint8_t cnt, uint8_t* pstep)
- #else
- void homeaxis(uint8_t axis, uint8_t cnt)
- #endif
- {
- bool endstops_enabled = enable_endstops(true);
- #define HOMEAXIS_DO(LETTER) \
- ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
- if ((axis==X_AXIS)?HOMEAXIS_DO(X):(axis==Y_AXIS)?HOMEAXIS_DO(Y):0)
- {
- int axis_home_dir = home_dir(axis);
- feedrate = homing_feedrate[axis];
- #ifdef TMC2130
- tmc2130_home_enter(X_AXIS_MASK << axis);
- #endif
-
-
-
- current_position[axis] = 0;
- plan_set_position_curposXYZE();
- set_destination_to_current();
- destination[axis] = -3.f * axis_home_dir;
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
-
- endstops_hit_on_purpose();
- enable_endstops(false);
- current_position[axis] = 0;
- plan_set_position_curposXYZE();
- destination[axis] = 1. * axis_home_dir;
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
-
- enable_endstops(true);
- destination[axis] = 1.1 * axis_home_dir * max_length(axis);
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- for (uint8_t i = 0; i < cnt; i++)
- {
-
- endstops_hit_on_purpose();
- enable_endstops(false);
- current_position[axis] = 0;
- plan_set_position_curposXYZE();
- destination[axis] = -10.f * axis_home_dir;
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- endstops_hit_on_purpose();
-
- enable_endstops(true);
- destination[axis] = 11.f * axis_home_dir;
- #ifdef TMC2130
- feedrate = homing_feedrate[axis];
- #else
- feedrate = homing_feedrate[axis] / 2;
- #endif
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- #ifdef TMC2130
- uint16_t mscnt = tmc2130_rd_MSCNT(axis);
- if (pstep) pstep[i] = mscnt >> 4;
- printf_P(PSTR("%3d step=%2d mscnt=%4d\n"), i, mscnt >> 4, mscnt);
- #endif
- }
- endstops_hit_on_purpose();
- enable_endstops(false);
- #ifdef TMC2130
- uint8_t orig = tmc2130_home_origin[axis];
- uint8_t back = tmc2130_home_bsteps[axis];
- if (tmc2130_home_enabled && (orig <= 63))
- {
- tmc2130_goto_step(axis, orig, 2, 1000, tmc2130_get_res(axis));
- if (back > 0)
- tmc2130_do_steps(axis, back, -axis_home_dir, 1000);
- }
- else
- tmc2130_do_steps(axis, 8, -axis_home_dir, 1000);
- tmc2130_home_exit();
- #endif
- axis_is_at_home(axis);
- axis_known_position[axis] = true;
-
- #ifdef TMC2130
- float dist = - axis_home_dir * 0.01f * tmc2130_home_fsteps[axis];
- #else
- float dist = - axis_home_dir * 0.01f * 64;
- #endif
- current_position[axis] -= dist;
- plan_set_position_curposXYZE();
- current_position[axis] += dist;
- destination[axis] = current_position[axis];
- plan_buffer_line_destinationXYZE(0.5f*feedrate/60);
- st_synchronize();
- feedrate = 0.0;
- }
- else if ((axis==Z_AXIS)?HOMEAXIS_DO(Z):0)
- {
- #ifdef TMC2130
- FORCE_HIGH_POWER_START;
- #endif
- int axis_home_dir = home_dir(axis);
- current_position[axis] = 0;
- plan_set_position_curposXYZE();
- destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
- feedrate = homing_feedrate[axis];
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- #ifdef TMC2130
- check_Z_crash();
- #endif
- current_position[axis] = 0;
- plan_set_position_curposXYZE();
- destination[axis] = -home_retract_mm(axis) * axis_home_dir;
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
- feedrate = homing_feedrate[axis]/2 ;
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- #ifdef TMC2130
- check_Z_crash();
- #endif
- axis_is_at_home(axis);
- destination[axis] = current_position[axis];
- feedrate = 0.0;
- endstops_hit_on_purpose();
- axis_known_position[axis] = true;
- #ifdef TMC2130
- FORCE_HIGH_POWER_END;
- #endif
- }
- enable_endstops(endstops_enabled);
- }
- void home_xy()
- {
- set_destination_to_current();
- homeaxis(X_AXIS);
- homeaxis(Y_AXIS);
- plan_set_position_curposXYZE();
- endstops_hit_on_purpose();
- }
- void refresh_cmd_timeout(void)
- {
- previous_millis_cmd.start();
- }
- #ifdef FWRETRACT
- void retract(bool retracting, bool swapretract = false) {
-
-
-
- if(retracting && !retracted[active_extruder]) {
- st_synchronize();
- set_destination_to_current();
- current_position[E_AXIS]+=(swapretract?retract_length_swap:cs.retract_length)*float(extrudemultiply)*0.01f;
- plan_set_e_position(current_position[E_AXIS]);
- float oldFeedrate = feedrate;
- feedrate=cs.retract_feedrate*60;
- retracted[active_extruder]=true;
- prepare_move();
- if(cs.retract_zlift) {
- st_synchronize();
- current_position[Z_AXIS]-=cs.retract_zlift;
- plan_set_position_curposXYZE();
- prepare_move();
- }
- feedrate = oldFeedrate;
- } else if(!retracting && retracted[active_extruder]) {
- st_synchronize();
- set_destination_to_current();
- float oldFeedrate = feedrate;
- feedrate=cs.retract_recover_feedrate*60;
- if(cs.retract_zlift) {
- current_position[Z_AXIS]+=cs.retract_zlift;
- plan_set_position_curposXYZE();
- prepare_move();
- st_synchronize();
- }
- current_position[E_AXIS]-=(swapretract?(retract_length_swap+retract_recover_length_swap):(cs.retract_length+cs.retract_recover_length))*float(extrudemultiply)*0.01f;
- plan_set_e_position(current_position[E_AXIS]);
- retracted[active_extruder]=false;
- prepare_move();
- feedrate = oldFeedrate;
- }
- }
- #endif
- #ifdef PRUSA_M28
- void trace() {
- Sound_MakeCustom(25,440,true);
- }
- #endif
- #ifdef TMC2130
- void force_high_power_mode(bool start_high_power_section) {
- #ifdef PSU_Delta
- if (start_high_power_section == true) enable_force_z();
- #endif
- uint8_t silent;
- silent = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
- if (silent == 1) {
-
-
- st_synchronize();
- cli();
- tmc2130_mode = (start_high_power_section == true) ? TMC2130_MODE_NORMAL : TMC2130_MODE_SILENT;
- update_mode_profile();
- tmc2130_init(TMCInitParams(FarmOrUserECool()));
-
-
- st_reset_timer();
- sei();
- }
- }
- #endif
- void gcode_M105(uint8_t extruder)
- {
- #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
- SERIAL_PROTOCOLPGM("T:");
- SERIAL_PROTOCOL_F(degHotend(extruder),1);
- SERIAL_PROTOCOLPGM(" /");
- SERIAL_PROTOCOL_F(degTargetHotend(extruder),1);
- #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
- SERIAL_PROTOCOLPGM(" B:");
- SERIAL_PROTOCOL_F(degBed(),1);
- SERIAL_PROTOCOLPGM(" /");
- SERIAL_PROTOCOL_F(degTargetBed(),1);
- #endif
- for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
- SERIAL_PROTOCOLPGM(" T");
- SERIAL_PROTOCOL(cur_extruder);
- SERIAL_PROTOCOL(':');
- SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
- SERIAL_PROTOCOLPGM(" /");
- SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
- }
- #else
- SERIAL_ERROR_START;
- SERIAL_ERRORLNRPGM(_i("No thermistors - no temperature"));
- #endif
- SERIAL_PROTOCOLPGM(" @:");
- #ifdef EXTRUDER_WATTS
- SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
- SERIAL_PROTOCOLPGM("W");
- #else
- SERIAL_PROTOCOL(getHeaterPower(extruder));
- #endif
- SERIAL_PROTOCOLPGM(" B@:");
- #ifdef BED_WATTS
- SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
- SERIAL_PROTOCOLPGM("W");
- #else
- SERIAL_PROTOCOL(getHeaterPower(-1));
- #endif
- #ifdef PINDA_THERMISTOR
- SERIAL_PROTOCOLPGM(" P:");
- SERIAL_PROTOCOL_F(current_temperature_pinda,1);
- #endif
- #ifdef AMBIENT_THERMISTOR
- SERIAL_PROTOCOLPGM(" A:");
- SERIAL_PROTOCOL_F(current_temperature_ambient,1);
- #endif
- #ifdef SHOW_TEMP_ADC_VALUES
- {
- float raw = 0.0;
- #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
- SERIAL_PROTOCOLPGM(" ADC B:");
- SERIAL_PROTOCOL_F(degBed(),1);
- SERIAL_PROTOCOLPGM("C->");
- raw = rawBedTemp();
- SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5);
- SERIAL_PROTOCOLPGM(" Rb->");
- SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5);
- SERIAL_PROTOCOLPGM(" Rxb->");
- SERIAL_PROTOCOL_F(raw, 5);
- #endif
- for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
- SERIAL_PROTOCOLPGM(" T");
- SERIAL_PROTOCOL(cur_extruder);
- SERIAL_PROTOCOLPGM(":");
- SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
- SERIAL_PROTOCOLPGM("C->");
- raw = rawHotendTemp(cur_extruder);
- SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5);
- SERIAL_PROTOCOLPGM(" Rt");
- SERIAL_PROTOCOL(cur_extruder);
- SERIAL_PROTOCOLPGM("->");
- SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5);
- SERIAL_PROTOCOLPGM(" Rx");
- SERIAL_PROTOCOL(cur_extruder);
- SERIAL_PROTOCOLPGM("->");
- SERIAL_PROTOCOL_F(raw, 5);
- }
- }
- #endif
- SERIAL_PROTOCOLLN();
- }
- #ifdef TMC2130
- static void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool calib, bool without_mbl)
- #else
- static void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool without_mbl)
- #endif
- {
-
- st_synchronize();
- homing_flag = true;
- #if 0
- SERIAL_ECHOPGM("G28, initial "); print_world_coordinates();
- SERIAL_ECHOPGM("G28, initial "); print_physical_coordinates();
- #endif
-
- bool home_x = home_x_axis;
- bool home_y = home_y_axis;
- bool home_z = home_z_axis;
-
- bool home_all_axes = home_x == home_y && home_x == home_z;
- if (home_all_axes)
-
- home_x = home_y = home_z = true;
-
- if (home_all_axes) {
- raise_z_above(MESH_HOME_Z_SEARCH);
- st_synchronize();
- }
- #ifdef ENABLE_AUTO_BED_LEVELING
- plan_bed_level_matrix.set_to_identity();
- #endif
-
-
-
-
- world2machine_revert_to_uncorrected();
-
-
-
-
- #ifdef MESH_BED_LEVELING
- uint8_t mbl_was_active = mbl.active;
- mbl.active = 0;
- current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
- #endif
-
- if (home_z)
- babystep_undo();
- int l_feedmultiply = setup_for_endstop_move();
- set_destination_to_current();
- feedrate = 0.0;
- #if Z_HOME_DIR > 0
- if(home_z)
- homeaxis(Z_AXIS);
- #endif
- #ifdef QUICK_HOME
-
- if(home_x && home_y)
- {
- current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
- uint8_t x_axis_home_dir = home_dir(X_AXIS);
- plan_set_position_curposXYZE();
- destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
- feedrate = homing_feedrate[X_AXIS];
- if(homing_feedrate[Y_AXIS]<feedrate)
- feedrate = homing_feedrate[Y_AXIS];
- if (max_length(X_AXIS) > max_length(Y_AXIS)) {
- feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
- } else {
- feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
- }
- plan_buffer_line_destinationXYZE(feedrate/60);
- st_synchronize();
- axis_is_at_home(X_AXIS);
- axis_is_at_home(Y_AXIS);
- plan_set_position_curposXYZE();
- destination[X_AXIS] = current_position[X_AXIS];
- destination[Y_AXIS] = current_position[Y_AXIS];
- plan_buffer_line_destinationXYZE(feedrate/60);
- feedrate = 0.0;
- st_synchronize();
- endstops_hit_on_purpose();
- current_position[X_AXIS] = destination[X_AXIS];
- current_position[Y_AXIS] = destination[Y_AXIS];
- current_position[Z_AXIS] = destination[Z_AXIS];
- }
- #endif
- #ifdef TMC2130
- if(home_x)
- {
- if (!calib)
- homeaxis(X_AXIS);
- else
- tmc2130_home_calibrate(X_AXIS);
- }
- if(home_y)
- {
- if (!calib)
- homeaxis(Y_AXIS);
- else
- tmc2130_home_calibrate(Y_AXIS);
- }
- #else
- if(home_x) homeaxis(X_AXIS);
- if(home_y) homeaxis(Y_AXIS);
- #endif
- if(home_x_axis && home_x_value != 0)
- current_position[X_AXIS]=home_x_value+cs.add_homing[X_AXIS];
- if(home_y_axis && home_y_value != 0)
- current_position[Y_AXIS]=home_y_value+cs.add_homing[Y_AXIS];
- #if Z_HOME_DIR < 0 // If homing towards BED do Z last
- #ifndef Z_SAFE_HOMING
- if(home_z) {
- #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
- raise_z_above(Z_RAISE_BEFORE_HOMING);
- st_synchronize();
- #endif
- #ifdef MESH_BED_LEVELING
- raise_z_above(MESH_HOME_Z_SEARCH);
- st_synchronize();
- if (!axis_known_position[X_AXIS]) homeaxis(X_AXIS);
- if (!axis_known_position[Y_AXIS]) homeaxis(Y_AXIS);
-
- world2machine_initialize();
- world2machine(pgm_read_float(bed_ref_points_4), pgm_read_float(bed_ref_points_4+1), destination[X_AXIS], destination[Y_AXIS]);
- world2machine_reset();
- if (destination[Y_AXIS] < Y_MIN_POS)
- destination[Y_AXIS] = Y_MIN_POS;
- feedrate = homing_feedrate[X_AXIS] / 20;
- enable_endstops(false);
- #ifdef DEBUG_BUILD
- SERIAL_ECHOLNPGM("plan_set_position()");
- MYSERIAL.println(current_position[X_AXIS]);MYSERIAL.println(current_position[Y_AXIS]);
- MYSERIAL.println(current_position[Z_AXIS]);MYSERIAL.println(current_position[E_AXIS]);
- #endif
- plan_set_position_curposXYZE();
- #ifdef DEBUG_BUILD
- SERIAL_ECHOLNPGM("plan_buffer_line()");
- MYSERIAL.println(destination[X_AXIS]);MYSERIAL.println(destination[Y_AXIS]);
- MYSERIAL.println(destination[Z_AXIS]);MYSERIAL.println(destination[E_AXIS]);
- MYSERIAL.println(feedrate);MYSERIAL.println(active_extruder);
- #endif
- plan_buffer_line_destinationXYZE(feedrate);
- st_synchronize();
- current_position[X_AXIS] = destination[X_AXIS];
- current_position[Y_AXIS] = destination[Y_AXIS];
- enable_endstops(true);
- endstops_hit_on_purpose();
- homeaxis(Z_AXIS);
- #else
- homeaxis(Z_AXIS);
- #endif
- }
- #else
- if(home_all_axes) {
- destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
- destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
- destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1);
- feedrate = XY_TRAVEL_SPEED/60;
- current_position[Z_AXIS] = 0;
- plan_set_position_curposXYZE();
- plan_buffer_line_destinationXYZE(feedrate);
- st_synchronize();
- current_position[X_AXIS] = destination[X_AXIS];
- current_position[Y_AXIS] = destination[Y_AXIS];
- homeaxis(Z_AXIS);
- }
-
- if(home_z) {
- if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
- && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
- && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
- && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
- && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
- current_position[Z_AXIS] = 0;
- plan_set_position_curposXYZE();
- destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1);
- feedrate = max_feedrate[Z_AXIS];
- plan_buffer_line_destinationXYZE(feedrate);
- st_synchronize();
- homeaxis(Z_AXIS);
- } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
- LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
- SERIAL_ECHO_START;
- SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
- } else {
- LCD_MESSAGERPGM(MSG_ZPROBE_OUT);
- SERIAL_ECHO_START;
- SERIAL_ECHOLNRPGM(MSG_ZPROBE_OUT);
- }
- }
- #endif
- #endif
- if(home_z_axis && home_z_value != 0)
- current_position[Z_AXIS]=home_z_value+cs.add_homing[Z_AXIS];
- #ifdef ENABLE_AUTO_BED_LEVELING
- if(home_z)
- current_position[Z_AXIS] += cs.zprobe_zoffset;
- #endif
-
-
-
-
- plan_set_position_curposXYZE();
- clean_up_after_endstop_move(l_feedmultiply);
- endstops_hit_on_purpose();
- #ifndef MESH_BED_LEVELING
-
-
- if(card.sdprinting && eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z))
- lcd_adjust_z();
- #endif
-
- world2machine_initialize();
-
- world2machine_update_current();
- #ifdef MESH_BED_LEVELING
- if (home_x_axis || home_y_axis || without_mbl || home_z_axis)
- {
- if (! home_z && mbl_was_active) {
-
- mbl.active = true;
-
- current_position[Z_AXIS] -= mbl.get_z(st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS));
- }
- }
- #endif
- if (farm_mode) { prusa_statistics(20); };
- st_synchronize();
- homing_flag = false;
- #if 0
- SERIAL_ECHOPGM("G28, final "); print_world_coordinates();
- SERIAL_ECHOPGM("G28, final "); print_physical_coordinates();
- SERIAL_ECHOPGM("G28, final "); print_mesh_bed_leveling_table();
- #endif
- }
- static void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis)
- {
- #ifdef TMC2130
- gcode_G28(home_x_axis, 0, home_y_axis, 0, home_z_axis, 0, false, true);
- #else
- gcode_G28(home_x_axis, 0, home_y_axis, 0, home_z_axis, 0, true);
- #endif
- }
- static void gcode_G80()
- {
- st_synchronize();
- if (waiting_inside_plan_buffer_line_print_aborted)
- return;
- mesh_bed_leveling_flag = true;
- #ifndef PINDA_THERMISTOR
- static bool run = false;
- #endif
- #ifdef SUPPORT_VERBOSITY
- int8_t verbosity_level = 0;
- if (code_seen('V')) {
-
- char c = strchr_pointer[1];
- verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
- }
- #endif
-
- if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
-
-
-
- repeatcommand_front();
- enquecommand_front_P(G28W0);
- return;
- }
- uint8_t nMeasPoints = MESH_MEAS_NUM_X_POINTS;
- if (code_seen('N')) {
- nMeasPoints = code_value_uint8();
- if (nMeasPoints != 7) {
- nMeasPoints = 3;
- }
- }
- else {
- nMeasPoints = eeprom_read_byte((uint8_t*)EEPROM_MBL_POINTS_NR);
- }
- uint8_t nProbeRetry = 3;
- if (code_seen('R')) {
- nProbeRetry = code_value_uint8();
- if (nProbeRetry > 10) {
- nProbeRetry = 10;
- }
- }
- else {
- nProbeRetry = eeprom_read_byte((uint8_t*)EEPROM_MBL_PROBE_NR);
- }
- bool magnet_elimination = (eeprom_read_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION) > 0);
- #ifndef PINDA_THERMISTOR
- if (run == false && eeprom_read_byte((uint8_t *)EEPROM_TEMP_CAL_ACTIVE) && calibration_status_pinda() == true && target_temperature_bed >= 50)
- {
- temp_compensation_start();
- run = true;
- repeatcommand_front();
- enquecommand_front_P(G28W0);
- break;
- }
- run = false;
- #endif
-
- CustomMsg custom_message_type_old = custom_message_type;
- uint8_t custom_message_state_old = custom_message_state;
- custom_message_type = CustomMsg::MeshBedLeveling;
- custom_message_state = (nMeasPoints * nMeasPoints) + 10;
- lcd_update(1);
- mbl.reset();
-
- babystep_undo();
-
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 60);
-
- current_position[X_AXIS] = BED_X0;
- current_position[Y_AXIS] = BED_Y0;
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 1)
- {
- bool clamped = world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
- clamped ? SERIAL_PROTOCOLPGM("First calibration point clamped.\n") : SERIAL_PROTOCOLPGM("No clamping for first calibration point.\n");
- }
- #else
- world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
- #endif
- int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
- plan_buffer_line_curposXYZE(XY_AXIS_FEEDRATE);
-
- st_synchronize();
- if (waiting_inside_plan_buffer_line_print_aborted)
- {
- custom_message_type = custom_message_type_old;
- custom_message_state = custom_message_state_old;
- return;
- }
- uint8_t mesh_point = 0;
- int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
- bool has_z = is_bed_z_jitter_data_valid();
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 1) {
- has_z ? SERIAL_PROTOCOLPGM("Z jitter data from Z cal. valid.\n") : SERIAL_PROTOCOLPGM("Z jitter data from Z cal. not valid.\n");
- }
- #endif
- int l_feedmultiply = setup_for_endstop_move(false);
- while (mesh_point != nMeasPoints * nMeasPoints) {
-
- uint8_t ix = mesh_point % nMeasPoints;
- uint8_t iy = mesh_point / nMeasPoints;
-
- if (iy & 1) ix = (nMeasPoints - 1) - ix;
- if (nMeasPoints == 7)
- {
- has_z = ((ix % 3 == 0) && (iy % 3 == 0)) && is_bed_z_jitter_data_valid();
- }
- float z0 = 0.f;
- if (has_z && (mesh_point > 0)) {
- uint16_t z_offset_u = 0;
- if (nMeasPoints == 7) {
- z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * ((ix/3) + iy - 1)));
- }
- else {
- z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * (ix + iy * 3 - 1)));
- }
- z0 = mbl.z_values[0][0] + *reinterpret_cast<int16_t*>(&z_offset_u) * 0.01;
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 1) {
- printf_P(PSTR("Bed leveling, point: %d, calibration Z stored in eeprom: %d, calibration z: %f \n"), mesh_point, z_offset_u, z0);
- }
- #endif
- }
-
- if((ix == 0) && (iy == 0)) current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- else current_position[Z_AXIS] += 2.f / nMeasPoints;
- float init_z_bckp = current_position[Z_AXIS];
- plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE);
- st_synchronize();
-
- current_position[X_AXIS] = BED_X(ix, nMeasPoints);
- current_position[Y_AXIS] = BED_Y(iy, nMeasPoints);
-
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 1) {
- bool clamped = world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
- SERIAL_PROTOCOL(mesh_point);
- clamped ? SERIAL_PROTOCOLPGM(": xy clamped.\n") : SERIAL_PROTOCOLPGM(": no xy clamping\n");
- }
- #else
- world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
- #endif
-
- plan_buffer_line_curposXYZE(XY_AXIS_FEEDRATE);
- st_synchronize();
- if (waiting_inside_plan_buffer_line_print_aborted)
- {
- custom_message_type = custom_message_type_old;
- custom_message_state = custom_message_state_old;
- return;
- }
-
- const float Z_CALIBRATION_THRESHOLD = 1.f;
- if (!find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f, nProbeRetry)) {
- printf_P(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
- break;
- }
- if (init_z_bckp - current_position[Z_AXIS] < 0.1f) {
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE);
- st_synchronize();
- if (!find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f, nProbeRetry)) {
- printf_P(_T(MSG_BED_LEVELING_FAILED_POINT_LOW));
- break;
- }
- if (MESH_HOME_Z_SEARCH - current_position[Z_AXIS] < 0.1f) {
- puts_P(PSTR("Bed leveling failed. Sensor disconnected or cable broken."));
- break;
- }
- }
- if (has_z && fabs(z0 - current_position[Z_AXIS]) > Z_CALIBRATION_THRESHOLD) {
- puts_P(PSTR("Bed leveling failed. Sensor triggered too high."));
- break;
- }
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 10) {
- SERIAL_ECHOPGM("X: ");
- MYSERIAL.print(current_position[X_AXIS], 5);
- SERIAL_ECHOLNPGM("");
- SERIAL_ECHOPGM("Y: ");
- MYSERIAL.print(current_position[Y_AXIS], 5);
- SERIAL_PROTOCOLPGM("\n");
- }
- #endif
- float offset_z = 0;
- #ifdef PINDA_THERMISTOR
- offset_z = temp_compensation_pinda_thermistor_offset(current_temperature_pinda);
- #endif
-
-
-
- mbl.set_z(ix, iy, current_position[Z_AXIS] - offset_z);
- custom_message_state--;
- mesh_point++;
- lcd_update(1);
- }
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 20) {
- SERIAL_ECHOLNPGM("Mesh bed leveling while loop finished.");
- SERIAL_ECHOLNPGM("MESH_HOME_Z_SEARCH: ");
- MYSERIAL.print(current_position[Z_AXIS], 5);
- }
- #endif
- plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE);
- st_synchronize();
- if (mesh_point != nMeasPoints * nMeasPoints) {
- Sound_MakeSound(e_SOUND_TYPE_StandardAlert);
- bool bState;
- do {
- lcd_display_message_fullscreen_P(_i("Some problem encountered, Z-leveling enforced ..."));
- #ifdef TMC2130
- lcd_wait_for_click_delay(MSG_BED_LEVELING_FAILED_TIMEOUT);
- calibrate_z_auto();
- #else
- lcd_wait_for_click_delay(0);
- lcd_calibrate_z_end_stop_manual(true);
- #endif
-
- bState=enable_z_endstop(false);
- current_position[Z_AXIS] -= 1;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
- enable_z_endstop(true);
- #ifdef TMC2130
- tmc2130_home_enter(Z_AXIS_MASK);
- #endif
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
- #ifdef TMC2130
- tmc2130_home_exit();
- #endif
- enable_z_endstop(bState);
- } while (st_get_position_mm(Z_AXIS) > MESH_HOME_Z_SEARCH);
-
- custom_message_type = custom_message_type_old;
- custom_message_state = custom_message_state_old;
- lcd_update_enable(true);
- gcode_G28(true, true, true);
- repeatcommand_front();
- return;
- }
- clean_up_after_endstop_move(l_feedmultiply);
-
- #ifndef PINDA_THERMISTOR
- if(eeprom_read_byte((uint8_t *)EEPROM_TEMP_CAL_ACTIVE) && calibration_status_pinda() == true) temp_compensation_apply();
- #endif
- babystep_apply();
-
- bool eeprom_bed_correction_valid = eeprom_read_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID) == 1;
- #ifdef SUPPORT_VERBOSITY
- if (verbosity_level >= 1) {
- eeprom_bed_correction_valid ? SERIAL_PROTOCOLPGM("Bed correction data valid\n") : SERIAL_PROTOCOLPGM("Bed correction data not valid\n");
- }
- #endif
- for (uint8_t i = 0; i < 4; ++i) {
- unsigned char codes[4] = { 'L', 'R', 'F', 'B' };
- long correction = 0;
- if (code_seen(codes[i]))
- correction = code_value_long();
- else if (eeprom_bed_correction_valid) {
- unsigned char *addr = (i < 2) ?
- ((i == 0) ? (unsigned char*)EEPROM_BED_CORRECTION_LEFT : (unsigned char*)EEPROM_BED_CORRECTION_RIGHT) :
- ((i == 2) ? (unsigned char*)EEPROM_BED_CORRECTION_FRONT : (unsigned char*)EEPROM_BED_CORRECTION_REAR);
- correction = eeprom_read_int8(addr);
- }
- if (correction == 0)
- continue;
- if (labs(correction) > BED_ADJUSTMENT_UM_MAX) {
- SERIAL_ERROR_START;
- SERIAL_ECHOPGM("Excessive bed leveling correction: ");
- SERIAL_ECHO(correction);
- SERIAL_ECHOLNPGM(" microns");
- }
- else {
- float offset = float(correction) * 0.001f;
- switch (i) {
- case 0:
- for (uint8_t row = 0; row < nMeasPoints; ++row) {
- for (uint8_t col = 0; col < nMeasPoints - 1; ++col) {
- mbl.z_values[row][col] += offset * (nMeasPoints - 1 - col) / (nMeasPoints - 1);
- }
- }
- break;
- case 1:
- for (uint8_t row = 0; row < nMeasPoints; ++row) {
- for (uint8_t col = 1; col < nMeasPoints; ++col) {
- mbl.z_values[row][col] += offset * col / (nMeasPoints - 1);
- }
- }
- break;
- case 2:
- for (uint8_t col = 0; col < nMeasPoints; ++col) {
- for (uint8_t row = 0; row < nMeasPoints; ++row) {
- mbl.z_values[row][col] += offset * (nMeasPoints - 1 - row) / (nMeasPoints - 1);
- }
- }
- break;
- case 3:
- for (uint8_t col = 0; col < nMeasPoints; ++col) {
- for (uint8_t row = 1; row < nMeasPoints; ++row) {
- mbl.z_values[row][col] += offset * row / (nMeasPoints - 1);
- }
- }
- break;
- }
- }
- }
-
- if (nMeasPoints == 3) {
- mbl.upsample_3x3();
- }
-
- if (nMeasPoints == 7 && magnet_elimination) {
- mbl_interpolation(nMeasPoints);
- }
-
-
- mbl.active = 1;
-
- go_home_with_z_lift();
-
-
- if (((int)degHotend(active_extruder) > extrude_min_temp) && eeprom_read_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE) && calibration_status_pinda() && (target_temperature_bed >= 50)) {
- current_position[E_AXIS] += default_retraction;
- plan_buffer_line_curposXYZE(400);
- }
- KEEPALIVE_STATE(NOT_BUSY);
-
- lcd_setstatuspgm(MSG_WELCOME);
- custom_message_type = custom_message_type_old;
- custom_message_state = custom_message_state_old;
- lcd_update(2);
- st_synchronize();
- mesh_bed_leveling_flag = false;
- }
- void adjust_bed_reset()
- {
- eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID, 1);
- eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_LEFT, 0);
- eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_RIGHT, 0);
- eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_FRONT, 0);
- eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_REAR, 0);
- }
- bool gcode_M45(bool onlyZ, int8_t verbosity_level)
- {
- bool final_result = false;
- #ifdef TMC2130
- FORCE_HIGH_POWER_START;
- #endif
-
- FORCE_BL_ON_START;
-
-
- if (!onlyZ)
- {
- setTargetBed(0);
- setAllTargetHotends(0);
- adjust_bed_reset();
- }
-
- lcd_update_enable(false);
-
- mbl.reset();
-
-
-
- world2machine_revert_to_uncorrected();
-
- babystep_reset();
-
- memset(axis_known_position, 0, sizeof(axis_known_position));
-
-
- int l_feedmultiply = setup_for_endstop_move();
- lcd_display_message_fullscreen_P(_T(MSG_AUTO_HOME));
- raise_z_above(MESH_HOME_Z_SEARCH);
- st_synchronize();
- home_xy();
- enable_endstops(false);
- current_position[X_AXIS] += 5;
- current_position[Y_AXIS] += 5;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
-
- #ifdef TMC2130
- if (calibrate_z_auto())
- {
- #else
- if (lcd_calibrate_z_end_stop_manual(onlyZ))
- {
- #endif
-
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_CONFIRM_NOZZLE_CLEAN));
- if(onlyZ){
- lcd_display_message_fullscreen_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1));
- lcd_puts_at_P(0,3,_n("1/9"));
- }else{
-
- lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1));
- lcd_puts_at_P(0,3,_n("1/4"));
- }
- refresh_cmd_timeout();
- #ifndef STEEL_SHEET
- if (((degHotend(0) > MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION)) && (!onlyZ))
- {
- lcd_wait_for_cool_down();
- }
- #endif
- if(!onlyZ)
- {
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- #ifdef STEEL_SHEET
- bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false);
- if(result) lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET));
- #endif
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_PAPER));
- KEEPALIVE_STATE(IN_HANDLER);
- lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1));
- lcd_puts_at_P(0,3,_n("1/4"));
- }
-
- bool endstops_enabled = enable_endstops(false);
- current_position[Z_AXIS] -= 1;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- enable_endstops(true);
- #ifdef TMC2130
- tmc2130_home_enter(Z_AXIS_MASK);
- #endif
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
- #ifdef TMC2130
- tmc2130_home_exit();
- #endif
- enable_endstops(endstops_enabled);
- if ((st_get_position_mm(Z_AXIS) <= (MESH_HOME_Z_SEARCH + HOME_Z_SEARCH_THRESHOLD)) &&
- (st_get_position_mm(Z_AXIS) >= (MESH_HOME_Z_SEARCH - HOME_Z_SEARCH_THRESHOLD)))
- {
- if (onlyZ)
- {
- clean_up_after_endstop_move(l_feedmultiply);
-
-
- world2machine_initialize();
-
- world2machine_update_current();
-
- bool result = sample_mesh_and_store_reference();
- if (result)
- {
- if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION)
- {
-
- calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
- }
- final_result = true;
-
- }
- }
- else
- {
-
- calibration_status_store(CALIBRATION_STATUS_XYZ_CALIBRATION);
- eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0);
-
- uint8_t point_too_far_mask = 0;
- BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level, point_too_far_mask);
- clean_up_after_endstop_move(l_feedmultiply);
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
- if (result >= 0)
- {
- #ifdef HEATBED_V2
- sample_z();
- #else
- point_too_far_mask = 0;
-
-
- mbl.reset();
- world2machine_reset();
-
- int l_feedmultiply = setup_for_endstop_move();
- home_xy();
- result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
- clean_up_after_endstop_move(l_feedmultiply);
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40);
- st_synchronize();
-
- #endif
- }
- lcd_update_enable(true);
- lcd_update(2);
- lcd_bed_calibration_show_result(result, point_too_far_mask);
- if (result >= 0)
- {
-
- calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
- if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) != 1) lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET));
- final_result = true;
- }
- }
- #ifdef TMC2130
- tmc2130_home_exit();
- #endif
- }
- else
- {
- lcd_show_fullscreen_message_and_wait_P(PSTR("Calibration failed! Check the axes and run again."));
- final_result = false;
- }
- }
- else
- {
-
- }
- lcd_update_enable(true);
- #ifdef TMC2130
- FORCE_HIGH_POWER_END;
- #endif
-
- FORCE_BL_ON_END;
-
- return final_result;
- }
- void gcode_M114()
- {
- SERIAL_PROTOCOLPGM("X:");
- SERIAL_PROTOCOL(current_position[X_AXIS]);
- SERIAL_PROTOCOLPGM(" Y:");
- SERIAL_PROTOCOL(current_position[Y_AXIS]);
- SERIAL_PROTOCOLPGM(" Z:");
- SERIAL_PROTOCOL(current_position[Z_AXIS]);
- SERIAL_PROTOCOLPGM(" E:");
- SERIAL_PROTOCOL(current_position[E_AXIS]);
- SERIAL_PROTOCOLRPGM(_n(" Count X: "));
- SERIAL_PROTOCOL(float(st_get_position(X_AXIS)) / cs.axis_steps_per_unit[X_AXIS]);
- SERIAL_PROTOCOLPGM(" Y:");
- SERIAL_PROTOCOL(float(st_get_position(Y_AXIS)) / cs.axis_steps_per_unit[Y_AXIS]);
- SERIAL_PROTOCOLPGM(" Z:");
- SERIAL_PROTOCOL(float(st_get_position(Z_AXIS)) / cs.axis_steps_per_unit[Z_AXIS]);
- SERIAL_PROTOCOLPGM(" E:");
- SERIAL_PROTOCOLLN(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]);
- }
- #if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
- void gcode_M123()
- {
- printf_P(_N("E0:%d RPM PRN1:%d RPM E0@:%u PRN1@:%d\n"), 60*fan_speed[active_extruder], 60*fan_speed[1], newFanSpeed, fanSpeed);
- }
- #endif
- template<typename T>
- static T gcode_M600_filament_change_z_shift()
- {
- #ifdef FILAMENTCHANGE_ZADD
- static_assert(Z_MAX_POS < (255 - FILAMENTCHANGE_ZADD), "Z-range too high, change the T type from uint8_t to uint16_t");
-
- T z_shift = T(FILAMENTCHANGE_ZADD);
- T ztmp = T( current_position[Z_AXIS] );
- if((ztmp + z_shift) < T(MIN_Z_FOR_SWAP)){
- z_shift = T(MIN_Z_FOR_SWAP) - ztmp;
- }
- return z_shift;
- #else
- return T(0);
- #endif
- }
- static void gcode_M600(bool automatic, float x_position, float y_position, float z_shift, float e_shift, float )
- {
- st_synchronize();
- float lastpos[4];
- if (farm_mode)
- {
- prusa_statistics(22);
- }
-
- int feedmultiplyBckp = feedmultiply;
- float HotendTempBckp = degTargetHotend(active_extruder);
- int fanSpeedBckp = fanSpeed;
- lastpos[X_AXIS] = current_position[X_AXIS];
- lastpos[Y_AXIS] = current_position[Y_AXIS];
- lastpos[Z_AXIS] = current_position[Z_AXIS];
- lastpos[E_AXIS] = current_position[E_AXIS];
-
- current_position[E_AXIS] += e_shift;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_RFEED);
- st_synchronize();
-
- current_position[Z_AXIS] += z_shift;
- clamp_to_software_endstops(current_position);
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_ZFEED);
- st_synchronize();
-
- current_position[X_AXIS] = x_position;
- current_position[Y_AXIS] = y_position;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_XYFEED);
- st_synchronize();
-
- if(!mmu_enabled) M600_wait_for_user(HotendTempBckp);
- lcd_change_fil_state = 0;
-
- if (mmu_enabled) extr_unload();
- else unload_filament(true);
-
- st_synchronize();
- if (!mmu_enabled)
- {
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- lcd_change_fil_state = lcd_show_fullscreen_message_yes_no_and_wait_P(
- _i("Was filament unload successful?"),
- false, true);
- if (lcd_change_fil_state == 0)
- {
- lcd_clear();
- lcd_puts_at_P(0, 2, _T(MSG_PLEASE_WAIT));
- current_position[X_AXIS] -= 100;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_XYFEED);
- st_synchronize();
- lcd_show_fullscreen_message_and_wait_P(_i("Please open idler and remove filament manually."));
- }
- }
- if (mmu_enabled)
- {
- if (!automatic) {
- if (saved_printing) mmu_eject_filament(mmu_extruder, false);
- mmu_M600_wait_and_beep();
- if (saved_printing) {
- lcd_clear();
- lcd_puts_at_P(0, 2, _T(MSG_PLEASE_WAIT));
- mmu_command(MmuCmd::R0);
- manage_response(false, false);
- }
- }
- mmu_M600_load_filament(automatic, HotendTempBckp);
- }
- else
- M600_load_filament();
- if (!automatic) M600_check_state(HotendTempBckp);
- lcd_update_enable(true);
-
- fanSpeed = fanSpeedBckp;
-
- if (!automatic)
- {
- current_position[E_AXIS] -= FILAMENTCHANGE_LOADRETRACT;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FIRST);
- st_synchronize();
- }
-
- plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
- FILAMENTCHANGE_XYFEED, active_extruder);
- st_synchronize();
-
- plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], current_position[E_AXIS],
- FILAMENTCHANGE_ZFEED, active_extruder);
- st_synchronize();
-
- if (!automatic)
- {
- current_position[E_AXIS] += FILAMENTCHANGE_LOADRETRACT;
- plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], current_position[E_AXIS],
- FILAMENTCHANGE_EFEED_FIRST, active_extruder);
- st_synchronize();
- }
-
- plan_set_e_position(lastpos[E_AXIS]);
- memcpy(current_position, lastpos, sizeof(lastpos));
- set_destination_to_current();
-
- feedmultiply = feedmultiplyBckp;
- char cmd[9];
- sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp);
- enquecommand(cmd);
- #ifdef IR_SENSOR
-
- fsensor_check_autoload();
- #endif
- lcd_setstatuspgm(MSG_WELCOME);
- custom_message_type = CustomMsg::Status;
- }
- void gcode_M701()
- {
- printf_P(PSTR("gcode_M701 begin\n"));
- if (farm_mode)
- {
- prusa_statistics(22);
- }
- if (mmu_enabled)
- {
- extr_adj(tmp_extruder);
- mmu_extruder = tmp_extruder;
- }
- else
- {
- enable_z();
- custom_message_type = CustomMsg::FilamentLoading;
- #ifdef FSENSOR_QUALITY
- fsensor_oq_meassure_start(40);
- #endif
- lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
- current_position[E_AXIS] += 40;
- plan_buffer_line_curposXYZE(400 / 60);
- st_synchronize();
- raise_z_above(MIN_Z_FOR_LOAD, false);
- current_position[E_AXIS] += 30;
- plan_buffer_line_curposXYZE(400 / 60);
-
- load_filament_final_feed();
- st_synchronize();
- Sound_MakeCustom(50,500,false);
- if (!farm_mode && loading_flag) {
- lcd_load_filament_color_check();
- }
- load_filament_final_retract();
- lcd_update_enable(true);
- lcd_update(2);
- lcd_setstatuspgm(MSG_WELCOME);
- disable_z();
- loading_flag = false;
- custom_message_type = CustomMsg::Status;
- #ifdef FSENSOR_QUALITY
- fsensor_oq_meassure_stop();
- if (!fsensor_oq_result())
- {
- bool disable = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Fil. sensor response is poor, disable it?"), false, true);
- lcd_update_enable(true);
- lcd_update(2);
- if (disable)
- fsensor_disable();
- }
- #endif
- }
- }
- #ifdef PRUSA_SN_SUPPORT
- static uint8_t get_PRUSA_SN(char* SN)
- {
- uint8_t selectedSerialPort_bak = selectedSerialPort;
- uint8_t rxIndex;
- bool SN_valid = false;
- ShortTimer timeout;
- selectedSerialPort = 0;
- timeout.start();
-
- while (!SN_valid)
- {
- rxIndex = 0;
- _delay(50);
- MYSERIAL.flush();
- SERIAL_ECHOLNRPGM(PSTR(";S"));
- while (rxIndex < 19)
- {
- if (timeout.expired(250u))
- goto exit;
- if (MYSERIAL.available() > 0)
- {
- SN[rxIndex] = MYSERIAL.read();
- rxIndex++;
- }
- }
- SN[rxIndex] = 0;
-
- SN_valid = (strncmp_P(SN, PSTR("CZPX"), 4) == 0);
- }
- exit:
- selectedSerialPort = selectedSerialPort_bak;
- return !SN_valid;
- }
- #endif
- static void gcode_PRUSA_BadRAMBoFanTest(){
-
- #if !defined(DEBUG_DISABLE_FANCHECK) && defined(FANCHECK) && defined(TACH_1) && TACH_1 >-1
- fan_measuring = false;
- unsigned long tach1max = 0;
- uint8_t tach1cntr = 0;
- for( ; tach1cntr < 100; ++tach1cntr){
-
- SET_OUTPUT(TACH_1);
- WRITE(TACH_1, LOW);
- _delay(20);
- unsigned long tachMeasure = _micros();
- cli();
- SET_INPUT(TACH_1);
-
-
- while( READ(TACH_1) == 0 ) ;
- sei();
- tachMeasure = _micros() - tachMeasure;
- if( tach1max < tachMeasure )
- tach1max = tachMeasure;
-
- }
-
- SERIAL_PROTOCOLPGM("RAMBo FAN ");
- if( tach1max > 500 ){
-
- SERIAL_PROTOCOLLNPGM("BAD");
- } else {
- SERIAL_PROTOCOLLNPGM("OK");
- }
-
- SET_INPUT(TACH_1);
- WRITE(TACH_1, HIGH);
- #endif
- }
- static void gcode_G92()
- {
- bool codes[NUM_AXIS];
- float values[NUM_AXIS];
-
- for(uint8_t i = 0; i < NUM_AXIS; ++i)
- {
- codes[i] = code_seen(axis_codes[i]);
- if(codes[i])
- values[i] = code_value();
- }
- if((codes[E_AXIS] && values[E_AXIS] == 0) &&
- (!codes[X_AXIS] && !codes[Y_AXIS] && !codes[Z_AXIS]))
- {
-
-
-
- current_position[E_AXIS] = 0;
- plan_reset_next_e();
- }
- else
- {
-
- st_synchronize();
- for(uint8_t i = 0; i < 3; ++i)
- {
- if(codes[i])
- current_position[i] = values[i] + cs.add_homing[i];
- }
- if(codes[E_AXIS])
- current_position[E_AXIS] = values[E_AXIS];
-
- plan_set_position_curposXYZE();
- }
- }
- #ifdef EXTENDED_CAPABILITIES_REPORT
- static void cap_line(const char* name, bool ena = false) {
- printf_P(PSTR("Cap:%S:%c\n"), name, (char)ena + '0');
- }
- static void extended_capabilities_report()
- {
-
- cap_line(PSTR("AUTOREPORT_TEMP"), ENABLED(AUTO_REPORT));
- #if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
-
- cap_line(PSTR("AUTOREPORT_FANS"), ENABLED(AUTO_REPORT));
- #endif
-
- cap_line(PSTR("AUTOREPORT_POSITION"), ENABLED(AUTO_REPORT));
-
- cap_line(PSTR("EXTENDED_M20"), 1);
- cap_line(PSTR("PRUSA_MMU2"), 1);
- }
- #endif
- #ifdef BACKLASH_X
- extern uint8_t st_backlash_x;
- #endif
- #ifdef BACKLASH_Y
- extern uint8_t st_backlash_y;
- #endif
- void process_commands()
- {
- #ifdef FANCHECK
- if(fan_check_error == EFCE_DETECTED) {
- fan_check_error = EFCE_REPORTED;
- if (usb_timer.running())
- lcd_pause_usb_print();
- else
- lcd_pause_print();
- }
- #endif
- if (!buflen) return;
- #ifdef CMDBUFFER_DEBUG
- SERIAL_ECHOPGM("Processing a GCODE command: ");
- SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
- SERIAL_ECHOLNPGM("");
- SERIAL_ECHOPGM("In cmdqueue: ");
- SERIAL_ECHO(buflen);
- SERIAL_ECHOLNPGM("");
- #endif
-
- unsigned long codenum;
- char *starpos = NULL;
- #ifdef ENABLE_AUTO_BED_LEVELING
- float x_tmp, y_tmp, z_tmp, real_z;
- #endif
-
- KEEPALIVE_STATE(IN_HANDLER);
-
- if (code_seen_P(PSTR("M117")))
- {
- starpos = (strchr(strchr_pointer + 5, '*'));
- if (starpos != NULL)
- *(starpos) = '\0';
- lcd_setstatus(strchr_pointer + 5);
- custom_message_type = CustomMsg::M117;
- }
-
- else if (code_seen_P(PSTR("M0")) || code_seen_P(PSTR("M1 "))) {
- char *src = strchr_pointer + 2;
- codenum = 0;
- bool hasP = false, hasS = false;
- if (code_seen('P')) {
- codenum = code_value_long();
- hasP = codenum > 0;
- }
- if (code_seen('S')) {
- codenum = code_value_long() * 1000;
- hasS = codenum > 0;
- }
- starpos = strchr(src, '*');
- if (starpos != NULL) *(starpos) = '\0';
- while (*src == ' ') ++src;
- custom_message_type = CustomMsg::M0Wait;
- if (!hasP && !hasS && *src != '\0') {
- lcd_setstatus(src);
- } else {
-
-
-
- if( ! farm_mode){
- LCD_MESSAGERPGM(_i("Wait for user..."));
- } else {
- custom_message_type = CustomMsg::Status;
- }
- }
- lcd_ignore_click();
- st_synchronize();
- previous_millis_cmd.start();
- if (codenum > 0 ) {
- codenum += _millis();
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- while(_millis() < codenum && !lcd_clicked()) {
- manage_heater();
- manage_inactivity(true);
- lcd_update(0);
- }
- KEEPALIVE_STATE(IN_HANDLER);
- lcd_ignore_click(false);
- } else {
- marlin_wait_for_click();
- }
- if (IS_SD_PRINTING)
- custom_message_type = CustomMsg::Status;
- else
- LCD_MESSAGERPGM(MSG_WELCOME);
- }
- #ifdef TMC2130
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("CRASH_"), 6) == 0)
- {
-
-
- if(code_seen_P(PSTR("CRASH_DETECTED")))
- {
- uint8_t mask = 0;
- if (code_seen('X')) mask |= X_AXIS_MASK;
- if (code_seen('Y')) mask |= Y_AXIS_MASK;
- crashdet_detected(mask);
- }
-
-
- else if(code_seen_P(PSTR("CRASH_RECOVER")))
- crashdet_recover();
-
-
- else if(code_seen_P(PSTR("CRASH_CANCEL")))
- crashdet_cancel();
- }
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("TMC_"), 4) == 0)
- {
-
-
-
- if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_WAVE_"), 9) == 0)
- {
- uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13);
- axis = (axis == 'E')?3:(axis - 'X');
- if (axis < 4)
- {
- uint8_t fac = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10);
- tmc2130_set_wave(axis, 247, fac);
- }
- }
-
-
-
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_STEP_"), 9) == 0)
- {
- uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13);
- axis = (axis == 'E')?3:(axis - 'X');
- if (axis < 4)
- {
- uint8_t step = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10);
- uint16_t res = tmc2130_get_res(axis);
- tmc2130_goto_step(axis, step & (4*res - 1), 2, 1000, res);
- }
- }
-
-
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_CHOP_"), 9) == 0)
- {
- uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13);
- axis = (axis == 'E')?3:(axis - 'X');
- if (axis < 4)
- {
- uint8_t chop0 = tmc2130_chopper_config[axis].toff;
- uint8_t chop1 = tmc2130_chopper_config[axis].hstr;
- uint8_t chop2 = tmc2130_chopper_config[axis].hend;
- uint8_t chop3 = tmc2130_chopper_config[axis].tbl;
- char* str_end = 0;
- if (CMDBUFFER_CURRENT_STRING[14])
- {
- chop0 = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, &str_end, 10) & 15;
- if (str_end && *str_end)
- {
- chop1 = (uint8_t)strtol(str_end, &str_end, 10) & 7;
- if (str_end && *str_end)
- {
- chop2 = (uint8_t)strtol(str_end, &str_end, 10) & 15;
- if (str_end && *str_end)
- chop3 = (uint8_t)strtol(str_end, &str_end, 10) & 3;
- }
- }
- }
- tmc2130_chopper_config[axis].toff = chop0;
- tmc2130_chopper_config[axis].hstr = chop1 & 7;
- tmc2130_chopper_config[axis].hend = chop2 & 15;
- tmc2130_chopper_config[axis].tbl = chop3 & 3;
- tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]);
-
- }
- }
- }
- #ifdef BACKLASH_X
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("BACKLASH_X"), 10) == 0)
- {
- uint8_t bl = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 10, NULL, 10);
- st_backlash_x = bl;
- printf_P(_N("st_backlash_x = %d\n"), st_backlash_x);
- }
- #endif
- #ifdef BACKLASH_Y
- else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("BACKLASH_Y"), 10) == 0)
- {
- uint8_t bl = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 10, NULL, 10);
- st_backlash_y = bl;
- printf_P(_N("st_backlash_y = %d\n"), st_backlash_y);
- }
- #endif
- #endif
- else if(code_seen_P(PSTR("PRUSA"))){
-
- if (code_seen_P(PSTR("Ping"))) {
- if (farm_mode) {
- PingTime = _millis();
- }
- }
- else if (code_seen_P(PSTR("PRN"))) {
- printf_P(_N("%u"), status_number);
- } else if( code_seen_P(PSTR("FANPINTST"))){
- gcode_PRUSA_BadRAMBoFanTest();
- }else if (code_seen_P(PSTR("FAN"))) {
- printf_P(_N("E0:%d RPM\nPRN0:%d RPM\n"), 60*fan_speed[0], 60*fan_speed[1]);
- }
- else if (code_seen_P(PSTR("thx")))
- {
- no_response = false;
- }
- else if (code_seen_P(PSTR("uvlo")))
- {
- eeprom_update_byte((uint8_t*)EEPROM_UVLO,0);
- enquecommand_P(PSTR("M24"));
- }
- else if (code_seen_P(PSTR("MMURES")))
- {
- mmu_reset();
- }
- else if (code_seen_P(PSTR("RESET"))) {
- #ifdef WATCHDOG
- #if defined(XFLASH) && defined(BOOTAPP)
- boot_app_magic = BOOT_APP_MAGIC;
- boot_app_flags = BOOT_APP_FLG_RUN;
- #endif
- softReset();
- #elif defined(BOOTAPP)
- asm volatile("jmp 0x3E000");
- #endif
- } else if (code_seen_P(PSTR("fv"))) {
-
- #ifdef SDSUPPORT
- card.openFileReadFilteredGcode(strchr_pointer + 3,true);
- while (true) {
- uint16_t readByte = card.getFilteredGcodeChar();
- MYSERIAL.write(readByte);
- if (readByte=='\n') {
- break;
- }
- }
- card.closefile();
- #endif
- }
- #ifdef PRUSA_M28
- else if (code_seen_P(PSTR("M28"))) {
- trace();
- prusa_sd_card_upload = true;
- card.openFileWrite(strchr_pointer+4);
- }
- #endif
- #ifdef PRUSA_SN_SUPPORT
- else if (code_seen_P(PSTR("SN"))) {
- char SN[20];
- eeprom_read_block(SN, (uint8_t*)EEPROM_PRUSA_SN, 20);
- if (SN[19])
- puts_P(PSTR("SN invalid"));
- else
- puts(SN);
- }
- #endif
- else if(code_seen_P(PSTR("Fir"))){
- SERIAL_PROTOCOLLN(FW_VERSION_FULL);
- } else if(code_seen_P(PSTR("Rev"))){
- SERIAL_PROTOCOLLN(FILAMENT_SIZE "-" ELECTRONICS "-" NOZZLE_TYPE );
- } else if(code_seen_P(PSTR("Lang"))) {
- lang_reset();
- } else if(code_seen_P(PSTR("Lz"))) {
- eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0);
- } else if(code_seen_P(PSTR("FR"))) {
-
- factory_reset(0);
- } else if(code_seen_P(PSTR("MBL"))) {
-
- if(code_seen('V')) {
- bool value = code_value_short();
- st_synchronize();
- if(value != mbl.active) {
- mbl.active = value;
-
- plan_set_z_position(current_position[Z_AXIS]);
- }
- }
- } else if (code_seen_P(PSTR("nozzle"))) {
- uint16_t nDiameter;
- if(code_seen('D'))
- {
- nDiameter=(uint16_t)(code_value()*1000.0+0.5);
- nozzle_diameter_check(nDiameter);
- }
- else if(code_seen_P(PSTR("set")) && farm_mode)
- {
- strchr_pointer++;
- strchr_pointer++;
- nDiameter=(uint16_t)(code_value()*1000.0+0.5);
- eeprom_update_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER,(uint8_t)ClNozzleDiameter::_Diameter_Undef);
- eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,nDiameter);
- }
- else SERIAL_PROTOCOLLN((float)eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM)/1000.0);
- }
-
-
-
- }
-
-
-
-
-
-
- else if(code_seen('G'))
- {
- gcode_in_progress = code_value_short();
- switch (gcode_in_progress)
- {
-
- case 0:
- case 1:
- if(Stopped == false) {
- get_coordinates();
-
-
-
- if ((saved_target[0] != SAVED_TARGET_UNSET) &&
- ((CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_SDCARD) ||
- (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR)))
- {
- memcpy(destination, saved_target, sizeof(destination));
- saved_target[0] = SAVED_TARGET_UNSET;
- }
- if (total_filament_used > ((current_position[E_AXIS] - destination[E_AXIS]) * 100)) {
- total_filament_used = total_filament_used + ((destination[E_AXIS] - current_position[E_AXIS]) * 100);
- }
- #ifdef FWRETRACT
- if(cs.autoretract_enabled) {
- if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
- float echange=destination[E_AXIS]-current_position[E_AXIS];
- if((echange<-MIN_RETRACT && !retracted[active_extruder]) || (echange>MIN_RETRACT && retracted[active_extruder])) {
- st_synchronize();
- current_position[E_AXIS] = destination[E_AXIS];
- plan_set_e_position(current_position[E_AXIS]);
- retract(!retracted[active_extruder]);
- return;
- }
- }
- }
- #endif
- prepare_move();
-
- }
- break;
-
- case 2:
- if(Stopped == false) {
- get_arc_coordinates();
- prepare_arc_move(true);
- }
- break;
-
-
- case 3:
- if(Stopped == false) {
- get_arc_coordinates();
- prepare_arc_move(false);
- }
- break;
-
- case 4:
- codenum = 0;
- if(code_seen('P')) codenum = code_value();
- if(code_seen('S')) codenum = code_value() * 1000;
- if(codenum != 0)
- {
- if(custom_message_type != CustomMsg::M117)
- {
- LCD_MESSAGERPGM(_n("Sleep..."));
- }
- }
- st_synchronize();
- codenum += _millis();
- previous_millis_cmd.start();
- while(_millis() < codenum) {
- manage_heater();
- manage_inactivity();
- lcd_update(0);
- }
- break;
- #ifdef FWRETRACT
-
- case 10:
- #if EXTRUDERS > 1
- retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1);
- retract(true,retracted_swap[active_extruder]);
- #else
- retract(true);
- #endif
- break;
-
- case 11:
- #if EXTRUDERS > 1
- retract(false,retracted_swap[active_extruder]);
- #else
- retract(false);
- #endif
- break;
- #endif
-
- case 21:
- break;
-
-
- case 28:
- {
- long home_x_value = 0;
- long home_y_value = 0;
- long home_z_value = 0;
-
- bool home_x = code_seen(axis_codes[X_AXIS]);
- home_x_value = code_value_long();
- bool home_y = code_seen(axis_codes[Y_AXIS]);
- home_y_value = code_value_long();
- bool home_z = code_seen(axis_codes[Z_AXIS]);
- home_z_value = code_value_long();
- bool without_mbl = code_seen('W');
-
- #ifdef TMC2130
- bool calib = code_seen('C');
- gcode_G28(home_x, home_x_value, home_y, home_y_value, home_z, home_z_value, calib, without_mbl);
- #else
- gcode_G28(home_x, home_x_value, home_y, home_y_value, home_z, home_z_value, without_mbl);
- #endif
- if ((home_x || home_y || without_mbl || home_z) == false) {
- gcode_G80();
- }
- break;
- }
- #ifdef ENABLE_AUTO_BED_LEVELING
-
-
- case 29:
- {
- #if Z_MIN_PIN == -1
- #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature! Z_MIN_PIN must point to a valid hardware pin."
- #endif
-
- if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
- {
- LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN);
- SERIAL_ECHO_START;
- SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN);
- break;
- }
- st_synchronize();
-
-
-
- plan_bed_level_matrix.set_to_identity();
- vector_3 uncorrected_position = plan_get_position();
-
- current_position[X_AXIS] = uncorrected_position.x;
- current_position[Y_AXIS] = uncorrected_position.y;
- current_position[Z_AXIS] = uncorrected_position.z;
- plan_set_position_curposXYZE();
- int l_feedmultiply = setup_for_endstop_move();
- feedrate = homing_feedrate[Z_AXIS];
- #ifdef AUTO_BED_LEVELING_GRID
-
- int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
- int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1);
-
-
-
-
-
-
- double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3];
-
- double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS];
- int probePointCounter = 0;
- bool zig = true;
- for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing)
- {
- int xProbe, xInc;
- if (zig)
- {
- xProbe = LEFT_PROBE_BED_POSITION;
-
- xInc = xGridSpacing;
- zig = false;
- } else
- {
- xProbe = RIGHT_PROBE_BED_POSITION;
-
- xInc = -xGridSpacing;
- zig = true;
- }
- for (int xCount=0; xCount < AUTO_BED_LEVELING_GRID_POINTS; xCount++)
- {
- float z_before;
- if (probePointCounter == 0)
- {
-
- z_before = Z_RAISE_BEFORE_PROBING;
- } else
- {
-
- z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
- }
- float measured_z = probe_pt(xProbe, yProbe, z_before);
- eqnBVector[probePointCounter] = measured_z;
- eqnAMatrix[probePointCounter + 0*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = xProbe;
- eqnAMatrix[probePointCounter + 1*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = yProbe;
- eqnAMatrix[probePointCounter + 2*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = 1;
- probePointCounter++;
- xProbe += xInc;
- }
- }
- clean_up_after_endstop_move(l_feedmultiply);
-
- double *plane_equation_coefficients = qr_solve(AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS, 3, eqnAMatrix, eqnBVector);
- SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
- SERIAL_PROTOCOL(plane_equation_coefficients[0]);
- SERIAL_PROTOCOLPGM(" b: ");
- SERIAL_PROTOCOL(plane_equation_coefficients[1]);
- SERIAL_PROTOCOLPGM(" d: ");
- SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
- set_bed_level_equation_lsq(plane_equation_coefficients);
- free(plane_equation_coefficients);
- #else
-
-
- float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
-
- float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
-
- float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
- clean_up_after_endstop_move(l_feedmultiply);
- set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
- #endif
- st_synchronize();
-
-
-
- real_z = float(st_get_position(Z_AXIS))/cs.axis_steps_per_unit[Z_AXIS];
- x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
- y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
- z_tmp = current_position[Z_AXIS];
- apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);
- current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS];
- plan_set_position_curposXYZE();
- }
- break;
- #ifndef Z_PROBE_SLED
-
- case 30:
- {
- st_synchronize();
-
- int l_feedmultiply = setup_for_endstop_move();
- feedrate = homing_feedrate[Z_AXIS];
- run_z_probe();
- SERIAL_PROTOCOLPGM(_T(MSG_BED));
- SERIAL_PROTOCOLPGM(" X: ");
- SERIAL_PROTOCOL(current_position[X_AXIS]);
- SERIAL_PROTOCOLPGM(" Y: ");
- SERIAL_PROTOCOL(current_position[Y_AXIS]);
- SERIAL_PROTOCOLPGM(" Z: ");
- SERIAL_PROTOCOL(current_position[Z_AXIS]);
- SERIAL_PROTOCOLPGM("\n");
- clean_up_after_endstop_move(l_feedmultiply);
- }
- break;
- #else
-
- case 31:
- dock_sled(true);
- break;
-
- case 32:
- dock_sled(false);
- break;
- #endif
- #endif
-
- #ifdef MESH_BED_LEVELING
-
- case 30:
- {
- st_synchronize();
- homing_flag = true;
-
- int l_feedmultiply = setup_for_endstop_move();
- feedrate = homing_feedrate[Z_AXIS];
- find_bed_induction_sensor_point_z(-10.f, 3);
- printf_P(_N("%S X: %.5f Y: %.5f Z: %.5f\n"), _T(MSG_BED), _x, _y, _z);
- clean_up_after_endstop_move(l_feedmultiply);
- homing_flag = false;
- }
- break;
-
-
- case 75:
- {
- for (uint8_t i = 40; i <= 110; i++)
- printf_P(_N("%d %.2f"), i, temp_comp_interpolation(i));
- }
- break;
-
- case 76:
- {
- #ifdef PINDA_THERMISTOR
- if (!has_temperature_compensation())
- {
- SERIAL_ECHOLNPGM("No PINDA thermistor");
- break;
- }
- if (calibration_status() >= CALIBRATION_STATUS_XYZ_CALIBRATION) {
-
-
- lcd_show_fullscreen_message_and_wait_P(_i("Please run XYZ calibration first."));
- break;
- }
- if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS]))
- {
-
-
-
- repeatcommand_front();
- enquecommand_front_P(G28W0);
- break;
- }
- lcd_show_fullscreen_message_and_wait_P(_i("Stable ambient temperature 21-26C is needed a rigid stand is required."));
- bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false);
- if (result)
- {
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[Z_AXIS] = 50;
- current_position[Y_AXIS] = 180;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET));
- current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
- current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- gcode_G28(false, false, true);
- }
- if ((current_temperature_pinda > 35) && (farm_mode == false)) {
-
- current_position[Z_AXIS] = 100;
- plan_buffer_line_curposXYZE(3000 / 60);
- if (lcd_wait_for_pinda(35) == false) {
- lcd_temp_cal_show_result(false);
- break;
- }
- }
- st_synchronize();
- homing_flag = true;
- lcd_update_enable(true);
- SERIAL_ECHOLNPGM("PINDA probe calibration start");
- float zero_z;
- int z_shift = 0;
- float start_temp = 5 * (int)(current_temperature_pinda / 5);
- if (start_temp < 35) start_temp = 35;
- if (start_temp < current_temperature_pinda) start_temp += 5;
- printf_P(_N("start temperature: %.1f\n"), start_temp);
- setTargetBed(70 + (start_temp - 30));
- custom_message_type = CustomMsg::TempCal;
- custom_message_state = 1;
- lcd_setstatuspgm(_T(MSG_PINDA_CALIBRATION));
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = PINDA_PREHEAT_X;
- current_position[Y_AXIS] = PINDA_PREHEAT_Y;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[Z_AXIS] = PINDA_PREHEAT_Z;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- while (current_temperature_pinda < start_temp)
- {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
- eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0);
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
- current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- bool find_z_result = find_bed_induction_sensor_point_z(-1.f);
- if (find_z_result == false) {
- lcd_temp_cal_show_result(find_z_result);
- homing_flag = false;
- break;
- }
- zero_z = current_position[Z_AXIS];
- printf_P(_N("\nZERO: %.3f\n"), current_position[Z_AXIS]);
- int i = -1; for (; i < 5; i++)
- {
- float temp = (40 + i * 5);
- printf_P(_N("\nStep: %d/6 (skipped)\nPINDA temperature: %d Z shift (mm):0\n"), i + 2, (40 + i*5));
- if (i >= 0) {
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
- }
- if (start_temp <= temp) break;
- }
- for (i++; i < 5; i++)
- {
- float temp = (40 + i * 5);
- printf_P(_N("\nStep: %d/6\n"), i + 2);
- custom_message_state = i + 2;
- setTargetBed(50 + 10 * (temp - 30) / 5);
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = PINDA_PREHEAT_X;
- current_position[Y_AXIS] = PINDA_PREHEAT_Y;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[Z_AXIS] = PINDA_PREHEAT_Z;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- while (current_temperature_pinda < temp)
- {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
- current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1);
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- find_z_result = find_bed_induction_sensor_point_z(-1.f);
- if (find_z_result == false) {
- lcd_temp_cal_show_result(find_z_result);
- break;
- }
- z_shift = (int)((current_position[Z_AXIS] - zero_z)*cs.axis_steps_per_unit[Z_AXIS]);
- printf_P(_N("\nPINDA temperature: %.1f Z shift (mm): %.3f"), current_temperature_pinda, current_position[Z_AXIS] - zero_z);
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
- }
- lcd_temp_cal_show_result(true);
- homing_flag = false;
- #else
- setTargetBed(PINDA_MIN_T);
- float zero_z;
- int z_shift = 0;
- int t_c;
- if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
-
-
-
- repeatcommand_front();
- enquecommand_front_P(G28W0);
- break;
- }
- puts_P(_N("PINDA probe calibration start"));
- custom_message_type = CustomMsg::TempCal;
- custom_message_state = 1;
- lcd_setstatuspgm(_T(MSG_PINDA_CALIBRATION));
- current_position[X_AXIS] = PINDA_PREHEAT_X;
- current_position[Y_AXIS] = PINDA_PREHEAT_Y;
- current_position[Z_AXIS] = PINDA_PREHEAT_Z;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
-
- while (fabs(degBed() - PINDA_MIN_T) > 1) {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
-
-
- for (int i = 0; i < PINDA_HEAT_T; i++) {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
- eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0);
- current_position[Z_AXIS] = 5;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = BED_X0;
- current_position[Y_AXIS] = BED_Y0;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
-
- find_bed_induction_sensor_point_z(-1.f);
- zero_z = current_position[Z_AXIS];
- printf_P(_N("\nZERO: %.3f\n"), current_position[Z_AXIS]);
- for (int i = 0; i<5; i++) {
- printf_P(_N("\nStep: %d/6\n"), i + 2);
- custom_message_state = i + 2;
- t_c = 60 + i * 10;
- setTargetBed(t_c);
- current_position[X_AXIS] = PINDA_PREHEAT_X;
- current_position[Y_AXIS] = PINDA_PREHEAT_Y;
- current_position[Z_AXIS] = PINDA_PREHEAT_Z;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- while (degBed() < t_c) {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
- for (int i = 0; i < PINDA_HEAT_T; i++) {
- delay_keep_alive(1000);
- serialecho_temperatures();
- }
- current_position[Z_AXIS] = 5;
- plan_buffer_line_curposXYZE(3000 / 60);
- current_position[X_AXIS] = BED_X0;
- current_position[Y_AXIS] = BED_Y0;
- plan_buffer_line_curposXYZE(3000 / 60);
- st_synchronize();
- find_bed_induction_sensor_point_z(-1.f);
- z_shift = (int)((current_position[Z_AXIS] - zero_z)*cs.axis_steps_per_unit[Z_AXIS]);
- printf_P(_N("\nTemperature: %d Z shift (mm): %.3f\n"), t_c, current_position[Z_AXIS] - zero_z);
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
-
-
- }
- custom_message_type = CustomMsg::Status;
- eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
- puts_P(_N("Temperature calibration done."));
- disable_x();
- disable_y();
- disable_z();
- disable_e0();
- disable_e1();
- disable_e2();
- setTargetBed(0);
- lcd_show_fullscreen_message_and_wait_P(_T(MSG_PINDA_CALIBRATION_DONE));
- eeprom_update_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE, 1);
- lcd_update_enable(true);
- lcd_update(2);
-
- #endif
- }
- break;
-
-
-
- case 80: {
- gcode_G80();
- }
- break;
-
- case 81:
- if (mbl.active) {
- SERIAL_PROTOCOLPGM("Num X,Y: ");
- SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
- SERIAL_PROTOCOL(',');
- SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
- SERIAL_PROTOCOLPGM("\nZ search height: ");
- SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH);
- SERIAL_PROTOCOLLNPGM("\nMeasured points:");
- for (uint8_t y = MESH_NUM_Y_POINTS; y-- > 0;) {
- for (uint8_t x = 0; x < MESH_NUM_X_POINTS; x++) {
- SERIAL_PROTOCOLPGM(" ");
- SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
- }
- SERIAL_PROTOCOLLN();
- }
- }
- else
- SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active.");
- break;
-
- #if 0
-
- case 82:
- SERIAL_PROTOCOLLNPGM("Finding bed ");
- int l_feedmultiply = setup_for_endstop_move();
- find_bed_induction_sensor_point_z();
- clean_up_after_endstop_move(l_feedmultiply);
- SERIAL_PROTOCOLPGM("Bed found at: ");
- SERIAL_PROTOCOL_F(current_position[Z_AXIS], 5);
- SERIAL_PROTOCOLPGM("\n");
- break;
-
- case 83:
- {
- int babystepz = code_seen('S') ? code_value() : 0;
- int BabyPosition = code_seen('P') ? code_value() : 0;
-
- if (babystepz != 0) {
-
-
- if (BabyPosition > 4) {
- SERIAL_PROTOCOLLNPGM("Index out of bounds");
- }else{
-
- babystepLoadZ = babystepz;
- eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z0 + BabyPosition, babystepLoadZ);
-
- babystepsTodoZadd(babystepLoadZ);
- }
-
- }
-
- }
- break;
-
- case 84:
- babystepsTodoZsubtract(babystepLoadZ);
-
- break;
-
-
- case 85:
- lcd_pick_babystep();
- break;
- #endif
-
-
- case 86:
- calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
- break;
-
-
- case 87:
- calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
- break;
-
-
- case 88:
- break;
- #endif
-
-
- case 90: {
- axis_relative_modes &= ~(X_AXIS_MASK | Y_AXIS_MASK | Z_AXIS_MASK);
- }
- break;
-
- case 91: {
- axis_relative_modes |= X_AXIS_MASK | Y_AXIS_MASK | Z_AXIS_MASK;
- }
- break;
-
- case 92: {
- gcode_G92();
- }
- break;
-
- case 98:
- farm_mode = 1;
- PingTime = _millis();
- eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode);
- SilentModeMenu = SILENT_MODE_OFF;
- eeprom_update_byte((unsigned char *)EEPROM_SILENT, SilentModeMenu);
- fCheckModeInit();
- break;
-
- case 99:
- farm_mode = 0;
- lcd_printer_connected();
- eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode);
- lcd_update(2);
- fCheckModeInit();
- break;
- default:
- printf_P(MSG_UNKNOWN_CODE, 'G', cmdbuffer + bufindr + CMDHDRSIZE);
- }
- gcode_in_progress = 0;
- }
-
-
- else if(code_seen('M'))
- {
- int index;
- for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
-
-
- if (*(strchr_pointer+index) < '0' || *(strchr_pointer+index) > '9') {
- printf_P(PSTR("Invalid M code: %s\n"), cmdbuffer + bufindr + CMDHDRSIZE);
- } else
- {
- mcode_in_progress = code_value_short();
- switch(mcode_in_progress)
- {
-
- case 17:
- LCD_MESSAGERPGM(_i("No move."));
- enable_x();
- enable_y();
- enable_z();
- enable_e0();
- enable_e1();
- enable_e2();
- break;
- #ifdef SDSUPPORT
-
- case 20:
- KEEPALIVE_STATE(NOT_BUSY);
- SERIAL_PROTOCOLLNRPGM(_N("Begin file list"));
- card.ls(CardReader::ls_param(code_seen('L'), code_seen('T')));
- SERIAL_PROTOCOLLNRPGM(_N("End file list"));
- break;
-
- case 21:
- card.initsd();
- break;
-
- case 22:
- card.release();
- break;
-
- case 23:
- starpos = (strchr(strchr_pointer + 4,'*'));
- if(starpos!=NULL)
- *(starpos)='\0';
- card.openFileReadFilteredGcode(strchr_pointer + 4, true);
- break;
-
- case 24:
- if (isPrintPaused)
- lcd_resume_print();
- else
- {
- if (!card.get_sdpos())
- {
-
- failstats_reset_print();
- #ifndef LA_NOCOMPAT
- la10c_reset();
- #endif
- }
- card.startFileprint();
- starttime=_millis();
- }
- break;
-
- case 26:
- if(card.cardOK && code_seen('S')) {
- long index = code_value_long();
- card.setIndex(index);
-
-
- sdpos_atomic = index;
- }
- break;
-
- case 27:
- card.getStatus(code_seen('P'));
- break;
-
- case 28:
- starpos = (strchr(strchr_pointer + 4,'*'));
- if(starpos != NULL){
- char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
- strchr_pointer = strchr(npos,' ') + 1;
- *(starpos) = '\0';
- }
- card.openFileWrite(strchr_pointer+4);
- break;
-
- case 29:
-
-
- break;
-
- case 30:
- if (card.cardOK){
- card.closefile();
- starpos = (strchr(strchr_pointer + 4,'*'));
- if(starpos != NULL){
- char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
- strchr_pointer = strchr(npos,' ') + 1;
- *(starpos) = '\0';
- }
- card.removeFile(strchr_pointer + 4);
- }
- break;
-
- case 32:
- {
- if(card.sdprinting) {
- st_synchronize();
- }
- starpos = (strchr(strchr_pointer + 4,'*'));
- char* namestartpos = (strchr(strchr_pointer + 4,'!'));
- if(namestartpos==NULL)
- {
- namestartpos=strchr_pointer + 4;
- }
- else
- namestartpos++;
- if(starpos!=NULL)
- *(starpos)='\0';
- bool call_procedure=(code_seen('P'));
- if(strchr_pointer>namestartpos)
- call_procedure=false;
- if( card.cardOK )
- {
- card.openFileReadFilteredGcode(namestartpos,!call_procedure);
- if(code_seen('S'))
- if(strchr_pointer<namestartpos)
- card.setIndex(code_value_long());
- card.startFileprint();
- if(!call_procedure)
- {
- if(!card.get_sdpos())
- {
-
- failstats_reset_print();
- #ifndef LA_NOCOMPAT
- la10c_reset();
- #endif
- }
- starttime=_millis();
- }
- }
- } break;
-
- case 928:
- starpos = (strchr(strchr_pointer + 5,'*'));
- if(starpos != NULL){
- char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N');
- strchr_pointer = strchr(npos,' ') + 1;
- *(starpos) = '\0';
- }
- card.openLogFile(strchr_pointer+5);
- break;
- #endif
-
- case 31:
- {
- stoptime=_millis();
- char time[30];
- unsigned long t=(stoptime-starttime)/1000;
- int sec,min;
- min=t/60;
- sec=t%60;
- sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
- SERIAL_ECHO_START;
- SERIAL_ECHOLN(time);
- lcd_setstatus(time);
- autotempShutdown();
- }
- break;
-
- case 42:
- if (code_seen('S'))
- {
- uint8_t pin_status = code_value_uint8();
- int8_t pin_number = LED_PIN;
- if (code_seen('P'))
- pin_number = code_value_uint8();
- for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(sensitive_pins[0])); i++)
- {
- if ((int8_t)pgm_read_byte(&sensitive_pins[i]) == pin_number)
- {
- pin_number = -1;
- break;
- }
- }
- #if defined(FAN_PIN) && FAN_PIN > -1
- if (pin_number == FAN_PIN)
- fanSpeed = pin_status;
- #endif
- if (pin_number > -1)
- {
- pinMode(pin_number, OUTPUT);
- digitalWrite(pin_number, pin_status);
- analogWrite(pin_number, pin_status);
- }
- }
- break;
-
- case 44:
-
- calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
- eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0);
-
- reset_bed_offset_and_skew();
-
-
-
- world2machine_revert_to_uncorrected();
- break;
-
- case 45:
- {
- int8_t verbosity_level = 0;
- bool only_Z = code_seen('Z');
- #ifdef SUPPORT_VERBOSITY
- if (code_seen('V'))
- {
-
- char c = strchr_pointer[1];
- verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
- }
- #endif
- gcode_M45(only_Z, verbosity_level);
- }
- break;
-
- case 46:
- {
-
- if (card.ToshibaFlashAir_isEnabled()) {
- uint8_t ip[4];
- if (card.ToshibaFlashAir_GetIP(ip)) {
-
- SERIAL_PROTOCOL(uint8_t(ip[0]));
- SERIAL_PROTOCOL('.');
- SERIAL_PROTOCOL(uint8_t(ip[1]));
- SERIAL_PROTOCOL('.');
- SERIAL_PROTOCOL(uint8_t(ip[2]));
- SERIAL_PROTOCOL('.');
- SERIAL_PROTOCOLLN(uint8_t(ip[3]));
- } else {
- SERIAL_PROTOCOLPGM("?Toshiba FlashAir GetIP failed\n");
- }
- } else {
- SERIAL_PROTOCOLLNPGM("n/a");
- }
- break;
- }
-
- case 47:
-
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- lcd_diag_show_end_stops();
- KEEPALIVE_STATE(IN_HANDLER);
- break;
- #if 0
- case 48:
- {
-
- lcd_update_enable(false);
-
- mbl.reset();
-
-
-
- world2machine_revert_to_uncorrected();
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
- st_synchronize();
-
- set_destination_to_current();
- int l_feedmultiply = setup_for_endstop_move();
- home_xy();
- int8_t verbosity_level = 0;
- if (code_seen('V')) {
-
- char c = strchr_pointer[1];
- verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
- }
- bool success = scan_bed_induction_points(verbosity_level);
- clean_up_after_endstop_move(l_feedmultiply);
-
- current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
- st_synchronize();
- lcd_update_enable(true);
- break;
- }
- #endif
- #ifdef ENABLE_AUTO_BED_LEVELING
- #ifdef Z_PROBE_REPEATABILITY_TEST
-
- case 48:
- {
- #if Z_MIN_PIN == -1
- #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
- #endif
- double sum=0.0;
- double mean=0.0;
- double sigma=0.0;
- double sample_set[50];
- int verbose_level=1, n=0, j, n_samples = 10, n_legs=0;
- double X_current, Y_current, Z_current;
- double X_probe_location, Y_probe_location, Z_start_location, ext_position;
-
- if (code_seen('V') || code_seen('v')) {
- verbose_level = code_value();
- if (verbose_level<0 || verbose_level>4 ) {
- SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
- goto Sigma_Exit;
- }
- }
- if (verbose_level > 0) {
- SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n");
- SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
- }
- if (code_seen('n')) {
- n_samples = code_value();
- if (n_samples<4 || n_samples>50 ) {
- SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n");
- goto Sigma_Exit;
- }
- }
- X_current = X_probe_location = st_get_position_mm(X_AXIS);
- Y_current = Y_probe_location = st_get_position_mm(Y_AXIS);
- Z_current = st_get_position_mm(Z_AXIS);
- Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
- ext_position = st_get_position_mm(E_AXIS);
- if (code_seen('X') || code_seen('x') ) {
- X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER;
- if (X_probe_location<X_MIN_POS || X_probe_location>X_MAX_POS ) {
- SERIAL_PROTOCOLPGM("?Specified X position out of range.\n");
- goto Sigma_Exit;
- }
- }
- if (code_seen('Y') || code_seen('y') ) {
- Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER;
- if (Y_probe_location<Y_MIN_POS || Y_probe_location>Y_MAX_POS ) {
- SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n");
- goto Sigma_Exit;
- }
- }
- if (code_seen('L') || code_seen('l') ) {
- n_legs = code_value();
- if ( n_legs==1 )
- n_legs = 2;
- if ( n_legs<0 || n_legs>15 ) {
- SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n");
- goto Sigma_Exit;
- }
- }
- st_synchronize();
- plan_bed_level_matrix.set_to_identity();
- plan_buffer_line( X_current, Y_current, Z_start_location,
- ext_position,
- homing_feedrate[Z_AXIS]/60,
- active_extruder);
- st_synchronize();
- if (verbose_level > 2)
- SERIAL_PROTOCOL("Positioning probe for the test.\n");
- plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
- ext_position,
- homing_feedrate[X_AXIS]/60,
- active_extruder);
- st_synchronize();
- current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
- current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS);
- current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
- current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS);
- int l_feedmultiply = setup_for_endstop_move();
- run_z_probe();
- current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
- Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
- plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
- ext_position,
- homing_feedrate[X_AXIS]/60,
- active_extruder);
- st_synchronize();
- current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
- for( n=0; n<n_samples; n++) {
- do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location);
- if ( n_legs) {
- double radius=0.0, theta=0.0, x_sweep, y_sweep;
- int rotational_direction, l;
- rotational_direction = (unsigned long) _millis() & 0x0001;
- radius = (unsigned long) _millis() % (long) (X_MAX_LENGTH/4);
- theta = (float) ((unsigned long) _millis() % (long) 360) / (360./(2*3.1415926));
- for( l=0; l<n_legs-1; l++) {
- if (rotational_direction==1)
- theta += (float) ((unsigned long) _millis() % (long) 20) / (360.0/(2*3.1415926));
- else
- theta -= (float) ((unsigned long) _millis() % (long) 20) / (360.0/(2*3.1415926));
- radius += (float) ( ((long) ((unsigned long) _millis() % (long) 10)) - 5);
- if ( radius<0.0 )
- radius = -radius;
- X_current = X_probe_location + cos(theta) * radius;
- Y_current = Y_probe_location + sin(theta) * radius;
- if ( X_current<X_MIN_POS)
- X_current = X_MIN_POS;
- if ( X_current>X_MAX_POS)
- X_current = X_MAX_POS;
- if ( Y_current<Y_MIN_POS)
- Y_current = Y_MIN_POS;
- if ( Y_current>Y_MAX_POS)
- Y_current = Y_MAX_POS;
- if (verbose_level>3 ) {
- SERIAL_ECHOPAIR("x: ", X_current);
- SERIAL_ECHOPAIR("y: ", Y_current);
- SERIAL_PROTOCOLLNPGM("");
- }
- do_blocking_move_to( X_current, Y_current, Z_current );
- }
- do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location);
- }
- int l_feedmultiply = setup_for_endstop_move();
- run_z_probe();
- sample_set[n] = current_position[Z_AXIS];
- sum=0.0;
- for( j=0; j<=n; j++) {
- sum = sum + sample_set[j];
- }
- mean = sum / (double (n+1));
- sum=0.0;
- for( j=0; j<=n; j++) {
- sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean);
- }
- sigma = sqrt( sum / (double (n+1)) );
- if (verbose_level > 1) {
- SERIAL_PROTOCOL(n+1);
- SERIAL_PROTOCOL(" of ");
- SERIAL_PROTOCOL(n_samples);
- SERIAL_PROTOCOLPGM(" z: ");
- SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6);
- }
- if (verbose_level > 2) {
- SERIAL_PROTOCOL(" mean: ");
- SERIAL_PROTOCOL_F(mean,6);
- SERIAL_PROTOCOL(" sigma: ");
- SERIAL_PROTOCOL_F(sigma,6);
- }
- if (verbose_level > 0)
- SERIAL_PROTOCOLPGM("\n");
- plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location,
- current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
- st_synchronize();
- }
- _delay(1000);
- clean_up_after_endstop_move(l_feedmultiply);
- if (verbose_level > 0) {
- SERIAL_PROTOCOLPGM("Mean: ");
- SERIAL_PROTOCOL_F(mean, 6);
- SERIAL_PROTOCOLPGM("\n");
- }
- SERIAL_PROTOCOLPGM("Standard Deviation: ");
- SERIAL_PROTOCOL_F(sigma, 6);
- SERIAL_PROTOCOLPGM("\n\n");
- Sigma_Exit:
- break;
- }
- #endif
- #endif
-
- case 73:
- {
- if(code_seen('P')) print_percent_done_normal = code_value_uint8();
- if(code_seen('R')) print_time_remaining_normal = code_value();
- if(code_seen('Q')) print_percent_done_silent = code_value_uint8();
- if(code_seen('S')) print_time_remaining_silent = code_value();
- if(code_seen('C')){
- float print_time_to_change_normal_f = code_value_float();
- print_time_to_change_normal = ( print_time_to_change_normal_f <= 0 ) ? PRINT_TIME_REMAINING_INIT : print_time_to_change_normal_f;
- }
- if(code_seen('D')){
- float print_time_to_change_silent_f = code_value_float();
- print_time_to_change_silent = ( print_time_to_change_silent_f <= 0 ) ? PRINT_TIME_REMAINING_INIT : print_time_to_change_silent_f;
- }
- {
- const char* _msg_mode_done_remain = _N("%S MODE: Percent done: %hhd; print time remaining in mins: %d; Change in mins: %d\n");
- printf_P(_msg_mode_done_remain, _N("NORMAL"), int8_t(print_percent_done_normal), print_time_remaining_normal, print_time_to_change_normal);
- printf_P(_msg_mode_done_remain, _N("SILENT"), int8_t(print_percent_done_silent), print_time_remaining_silent, print_time_to_change_silent);
- }
- break;
- }
-
- case 104:
- {
- uint8_t extruder;
- if(setTargetedHotend(104,extruder)){
- break;
- }
- if (code_seen('S'))
- {
- setTargetHotendSafe(code_value(), extruder);
- }
- break;
- }
-
- case 112:
- kill(MSG_M112_KILL, 3);
- break;
-
- case 140:
- if (code_seen('S')) setTargetBed(code_value());
- break;
-
- case 105:
- {
- uint8_t extruder;
- if(setTargetedHotend(105, extruder)){
- break;
- }
-
- SERIAL_PROTOCOLPGM("ok ");
- gcode_M105(extruder);
-
- cmdqueue_pop_front();
- cmdbuffer_front_already_processed = true;
-
- break;
- }
- #if defined(AUTO_REPORT)
-
- case 155:
- {
- if (code_seen('S')){
- autoReportFeatures.SetPeriod( code_value_uint8() );
- }
- if (code_seen('C')){
- autoReportFeatures.SetMask(code_value_uint8());
- } else{
- autoReportFeatures.SetMask(1);
- }
- }
- break;
- #endif
-
- case 109:
- {
- uint8_t extruder;
- if(setTargetedHotend(109, extruder)){
- break;
- }
- LCD_MESSAGERPGM(_T(MSG_HEATING));
- heating_status = HeatingStatus::EXTRUDER_HEATING;
- if (farm_mode) { prusa_statistics(1); };
- #ifdef AUTOTEMP
- autotemp_enabled=false;
- #endif
- if (code_seen('S')) {
- setTargetHotendSafe(code_value(), extruder);
- } else if (code_seen('R')) {
- setTargetHotendSafe(code_value(), extruder);
- }
- #ifdef AUTOTEMP
- if (code_seen('S')) autotemp_min=code_value();
- if (code_seen('B')) autotemp_max=code_value();
- if (code_seen('F'))
- {
- autotemp_factor=code_value();
- autotemp_enabled=true;
- }
- #endif
- codenum = _millis();
-
- target_direction = isHeatingHotend(extruder);
- cancel_heatup = false;
- wait_for_heater(codenum, extruder);
- LCD_MESSAGERPGM(_T(MSG_HEATING_COMPLETE));
- heating_status = HeatingStatus::EXTRUDER_HEATING_COMPLETE;
- if (farm_mode) { prusa_statistics(2); };
-
-
- previous_millis_cmd.start();
- }
- break;
-
- case 190:
- #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
- {
- bool CooldownNoWait = false;
- LCD_MESSAGERPGM(_T(MSG_BED_HEATING));
- heating_status = HeatingStatus::BED_HEATING;
- if (farm_mode) { prusa_statistics(1); };
- if (code_seen('S'))
- {
- setTargetBed(code_value());
- CooldownNoWait = true;
- }
- else if (code_seen('R'))
- {
- setTargetBed(code_value());
- }
- codenum = _millis();
-
- cancel_heatup = false;
- target_direction = isHeatingBed();
- while ( (!cancel_heatup) && (target_direction ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false))) )
- {
- if(( _millis() - codenum) > 1000 )
- {
- if (!farm_mode) {
- float tt = degHotend(active_extruder);
- SERIAL_PROTOCOLPGM("T:");
- SERIAL_PROTOCOL(tt);
- SERIAL_PROTOCOLPGM(" E:");
- SERIAL_PROTOCOL((int)active_extruder);
- SERIAL_PROTOCOLPGM(" B:");
- SERIAL_PROTOCOL_F(degBed(), 1);
- SERIAL_PROTOCOLLN();
- }
- codenum = _millis();
-
- }
- manage_heater();
- manage_inactivity();
- lcd_update(0);
- }
- LCD_MESSAGERPGM(_T(MSG_BED_DONE));
- heating_status = HeatingStatus::BED_HEATING_COMPLETE;
- previous_millis_cmd.start();
- }
- #endif
- break;
- #if defined(FAN_PIN) && FAN_PIN > -1
-
- case 106:
- if (code_seen('S')){
- fanSpeed = code_value_uint8();
- }
- else {
- fanSpeed = 255;
- }
- break;
-
- case 107:
- fanSpeed = 0;
- break;
- #endif
- #if defined(PS_ON_PIN) && PS_ON_PIN > -1
-
- case 80:
- SET_OUTPUT(PS_ON_PIN);
- WRITE(PS_ON_PIN, PS_ON_AWAKE);
-
-
-
- #if defined SUICIDE_PIN && SUICIDE_PIN > -1
- SET_OUTPUT(SUICIDE_PIN);
- WRITE(SUICIDE_PIN, HIGH);
- #endif
- powersupply = true;
- LCD_MESSAGERPGM(MSG_WELCOME);
- lcd_update(0);
- break;
-
- case 81:
- disable_heater();
- st_synchronize();
- disable_e0();
- disable_e1();
- disable_e2();
- finishAndDisableSteppers();
- fanSpeed = 0;
- _delay(1000);
- #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
- st_synchronize();
- suicide();
- #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
- SET_OUTPUT(PS_ON_PIN);
- WRITE(PS_ON_PIN, PS_ON_ASLEEP);
- #endif
- powersupply = false;
- LCD_MESSAGERPGM(CAT4(CUSTOM_MENDEL_NAME,PSTR(" "),MSG_OFF,PSTR(".")));
- lcd_update(0);
- break;
- #endif
-
- case 82:
- axis_relative_modes &= ~E_AXIS_MASK;
- break;
-
- case 83:
- axis_relative_modes |= E_AXIS_MASK;
- break;
-
- case 18:
- case 84:
- if(code_seen('S')){
- stepper_inactive_time = code_value() * 1000;
- }
- else
- {
- bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS]))|| (code_seen(axis_codes[E_AXIS])));
- if(all_axis)
- {
- st_synchronize();
- disable_e0();
- disable_e1();
- disable_e2();
- finishAndDisableSteppers();
- }
- else
- {
- st_synchronize();
- if (code_seen('X')) disable_x();
- if (code_seen('Y')) disable_y();
- if (code_seen('Z')) disable_z();
- #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN))
- if (code_seen('E')) {
- disable_e0();
- disable_e1();
- disable_e2();
- }
- #endif
- }
- }
- break;
-
- case 85:
- if(code_seen('S')) {
- max_inactive_time = code_value() * 1000;
- }
- break;
- #ifdef SAFETYTIMER
-
- case 86:
- if (code_seen('S')) {
- safetytimer_inactive_time = code_value() * 1000;
- safetyTimer.start();
- }
- break;
- #endif
-
- case 92:
- for(int8_t i=0; i < NUM_AXIS; i++)
- {
- if(code_seen(axis_codes[i]))
- {
- if(i == E_AXIS) {
- float value = code_value();
- if(value < 20.0) {
- float factor = cs.axis_steps_per_unit[i] / value;
- cs.max_jerk[E_AXIS] *= factor;
- max_feedrate[i] *= factor;
- axis_steps_per_sqr_second[i] *= factor;
- }
- cs.axis_steps_per_unit[i] = value;
- #if defined(FILAMENT_SENSOR) && defined(PAT9125)
- fsensor_set_axis_steps_per_unit(value);
- #endif
- }
- else {
- cs.axis_steps_per_unit[i] = code_value();
- }
- }
- }
- reset_acceleration_rates();
- break;
-
- case 110:
- if (code_seen('N'))
- gcode_LastN = code_value_long();
- break;
-
- case 113:
- if (code_seen('S')) {
- host_keepalive_interval = code_value_uint8();
- }
- else {
- SERIAL_ECHO_START;
- SERIAL_ECHOPAIR("M113 S", (unsigned long)host_keepalive_interval);
- SERIAL_PROTOCOLLN();
- }
- break;
-
- case 115:
- if (code_seen('V')) {
-
- SERIAL_PROTOCOLLNRPGM(FW_VERSION_STR_P());
- } else if (code_seen('U')) {
-
-
- show_upgrade_dialog_if_version_newer(++ strchr_pointer);
- } else {
- SERIAL_ECHOPGM("FIRMWARE_NAME:Prusa-Firmware ");
- SERIAL_ECHORPGM(FW_VERSION_STR_P());
- SERIAL_ECHOPGM(" based on Marlin FIRMWARE_URL:https://github.com/prusa3d/Prusa-Firmware PROTOCOL_VERSION:");
- SERIAL_ECHOPGM(PROTOCOL_VERSION);
- SERIAL_ECHOPGM(" MACHINE_TYPE:");
- SERIAL_ECHOPGM(CUSTOM_MENDEL_NAME);
- SERIAL_ECHOPGM(" EXTRUDER_COUNT:");
- SERIAL_ECHOPGM(STRINGIFY(EXTRUDERS));
- SERIAL_ECHOPGM(" UUID:");
- SERIAL_ECHOLNPGM(MACHINE_UUID);
- #ifdef EXTENDED_CAPABILITIES_REPORT
- extended_capabilities_report();
- #endif
- }
- break;
-
- case 114:
- gcode_M114();
- break;
-
-
- #ifdef M120_M121_ENABLED
-
- case 120:
- enable_endstops(true) ;
- break;
-
- case 121:
- enable_endstops(false) ;
- break;
- #endif
-
- case 119:
- SERIAL_PROTOCOLRPGM(_N("Reporting endstop status"));
- SERIAL_PROTOCOLLN();
- #if defined(X_MIN_PIN) && X_MIN_PIN > -1
- SERIAL_PROTOCOLRPGM(_n("x_min: "));
- if(READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- #if defined(X_MAX_PIN) && X_MAX_PIN > -1
- SERIAL_PROTOCOLRPGM(_n("x_max: "));
- if(READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
- SERIAL_PROTOCOLRPGM(_n("y_min: "));
- if(READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
- SERIAL_PROTOCOLRPGM(_n("y_max: "));
- if(READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
- SERIAL_PROTOCOLRPGM(MSG_Z_MIN);
- if(READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
- SERIAL_PROTOCOLRPGM(MSG_Z_MAX);
- if(READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING){
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT);
- }else{
- SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN);
- }
- SERIAL_PROTOCOLLN();
- #endif
- break;
-
- #if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
-
- case 123:
- gcode_M123();
- break;
- #endif
- #ifdef BLINKM
-
- case 150:
- {
- byte red;
- byte grn;
- byte blu;
- if(code_seen('R')) red = code_value();
- if(code_seen('U')) grn = code_value();
- if(code_seen('B')) blu = code_value();
- SendColors(red,grn,blu);
- }
- break;
- #endif
-
- case 200:
- {
- uint8_t extruder = active_extruder;
- if(code_seen('T')) {
- extruder = code_value_uint8();
- if(extruder >= EXTRUDERS) {
- SERIAL_ECHO_START;
- SERIAL_ECHO(_n("M200 Invalid extruder "));
- break;
- }
- }
- if(code_seen('D')) {
- float diameter = code_value();
- if (diameter == 0.0) {
-
-
-
- cs.volumetric_enabled = false;
- } else {
- cs.filament_size[extruder] = code_value();
-
- cs.filament_size[0] = (cs.filament_size[0] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[0]);
- #if EXTRUDERS > 1
- cs.filament_size[1] = (cs.filament_size[1] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[1]);
- #if EXTRUDERS > 2
- cs.filament_size[2] = (cs.filament_size[2] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[2]);
- #endif
- #endif
- cs.volumetric_enabled = true;
- }
- } else {
-
- break;
- }
- calculate_extruder_multipliers();
- }
- break;
-
- case 201:
- for (int8_t i = 0; i < NUM_AXIS; i++)
- {
- if (code_seen(axis_codes[i]))
- {
- unsigned long val = code_value();
- #ifdef TMC2130
- unsigned long val_silent = val;
- if ((i == X_AXIS) || (i == Y_AXIS))
- {
- if (val > NORMAL_MAX_ACCEL_XY)
- val = NORMAL_MAX_ACCEL_XY;
- if (val_silent > SILENT_MAX_ACCEL_XY)
- val_silent = SILENT_MAX_ACCEL_XY;
- }
- cs.max_acceleration_units_per_sq_second_normal[i] = val;
- cs.max_acceleration_units_per_sq_second_silent[i] = val_silent;
- #else
- max_acceleration_units_per_sq_second[i] = val;
- #endif
- }
- }
-
- reset_acceleration_rates();
- break;
- #if 0
- case 202:
- for(int8_t i=0; i < NUM_AXIS; i++) {
- if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * cs.axis_steps_per_unit[i];
- }
- break;
- #endif
-
- case 203:
- for (uint8_t i = 0; i < NUM_AXIS; i++)
- {
- if (code_seen(axis_codes[i]))
- {
- float val = code_value();
- #ifdef TMC2130
- float val_silent = val;
- if ((i == X_AXIS) || (i == Y_AXIS))
- {
- if (val > NORMAL_MAX_FEEDRATE_XY)
- val = NORMAL_MAX_FEEDRATE_XY;
- if (val_silent > SILENT_MAX_FEEDRATE_XY)
- val_silent = SILENT_MAX_FEEDRATE_XY;
- }
- cs.max_feedrate_normal[i] = val;
- cs.max_feedrate_silent[i] = val_silent;
- #else
- max_feedrate[i] = val;
- #endif
- }
- }
- break;
-
- case 204:
- {
- if(code_seen('S')) {
-
-
-
- cs.acceleration = cs.travel_acceleration = code_value();
-
- if(code_seen('T'))
- cs.retract_acceleration = code_value();
- } else {
-
- if(code_seen('P'))
- cs.acceleration = code_value();
- if(code_seen('R'))
- cs.retract_acceleration = code_value();
- if(code_seen('T'))
- cs.travel_acceleration = code_value();
- }
- }
- break;
-
- case 205:
- {
- if(code_seen('S')) cs.minimumfeedrate = code_value();
- if(code_seen('T')) cs.mintravelfeedrate = code_value();
- if(code_seen('B')) cs.minsegmenttime = code_value() ;
- if(code_seen('X')) cs.max_jerk[X_AXIS] = cs.max_jerk[Y_AXIS] = code_value();
- if(code_seen('Y')) cs.max_jerk[Y_AXIS] = code_value();
- if(code_seen('Z')) cs.max_jerk[Z_AXIS] = code_value();
- if(code_seen('E'))
- {
- float e = code_value();
- #ifndef LA_NOCOMPAT
- e = la10c_jerk(e);
- #endif
- cs.max_jerk[E_AXIS] = e;
- }
- }
- break;
-
- case 206:
- for(uint8_t i=0; i < 3; i++)
- {
- if(code_seen(axis_codes[i])) cs.add_homing[i] = code_value();
- }
- break;
- #ifdef FWRETRACT
-
- case 207:
- {
- if(code_seen('S'))
- {
- cs.retract_length = code_value() ;
- }
- if(code_seen('F'))
- {
- cs.retract_feedrate = code_value()/60 ;
- }
- if(code_seen('Z'))
- {
- cs.retract_zlift = code_value() ;
- }
- }break;
-
- case 208:
- {
- if(code_seen('S'))
- {
- cs.retract_recover_length = code_value() ;
- }
- if(code_seen('F'))
- {
- cs.retract_recover_feedrate = code_value()/60 ;
- }
- }break;
-
- case 209:
- {
- if(code_seen('S'))
- {
- switch(code_value_uint8())
- {
- case 0:
- {
- cs.autoretract_enabled=false;
- retracted[0]=false;
- #if EXTRUDERS > 1
- retracted[1]=false;
- #endif
- #if EXTRUDERS > 2
- retracted[2]=false;
- #endif
- }break;
- case 1:
- {
- cs.autoretract_enabled=true;
- retracted[0]=false;
- #if EXTRUDERS > 1
- retracted[1]=false;
- #endif
- #if EXTRUDERS > 2
- retracted[2]=false;
- #endif
- }break;
- default:
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
- SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
- SERIAL_ECHOLNPGM("\"(1)");
- }
- }
- }break;
- #endif
-
- case 214:
- {
-
- float p = code_seen('P') ? code_value_float() : cs.mm_per_arc_segment;
- float s = code_seen('S') ? code_value_float() : cs.min_mm_per_arc_segment;
- unsigned char n = code_seen('N') ? code_value() : cs.n_arc_correction;
- unsigned short r = code_seen('R') ? code_value() : cs.min_arc_segments;
- unsigned short f = code_seen('F') ? code_value() : cs.arc_segments_per_sec;
-
- if (p <=0 || s < 0 || p < s)
- {
-
- break;
- }
- cs.mm_per_arc_segment = p;
- cs.min_mm_per_arc_segment = s;
- cs.n_arc_correction = n;
- cs.min_arc_segments = r;
- cs.arc_segments_per_sec = f;
- }break;
- #if EXTRUDERS > 1
-
- case 218:
- {
- uint8_t extruder;
- if(setTargetedHotend(218, extruder)){
- break;
- }
- if(code_seen('X'))
- {
- extruder_offset[X_AXIS][extruder] = code_value();
- }
- if(code_seen('Y'))
- {
- extruder_offset[Y_AXIS][extruder] = code_value();
- }
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(MSG_HOTEND_OFFSET);
- for(extruder = 0; extruder < EXTRUDERS; extruder++)
- {
- SERIAL_ECHO(" ");
- SERIAL_ECHO(extruder_offset[X_AXIS][extruder]);
- SERIAL_ECHO(",");
- SERIAL_ECHO(extruder_offset[Y_AXIS][extruder]);
- }
- SERIAL_ECHOLN("");
- }break;
- #endif
-
- case 220:
- {
- bool codesWereSeen = false;
- if (code_seen('B'))
- {
- saved_feedmultiply_mm = feedmultiply;
- codesWereSeen = true;
- }
- if (code_seen('S'))
- {
- feedmultiply = code_value_short();
- codesWereSeen = true;
- }
- if (code_seen('R'))
- {
- feedmultiply = saved_feedmultiply_mm;
- codesWereSeen = true;
- }
- if (!codesWereSeen)
- {
- printf_P(PSTR("%i%%\n"), feedmultiply);
- }
- }
- break;
-
- case 221:
- {
- if (code_seen('S'))
- {
- int tmp_code = code_value_short();
- if (code_seen('T'))
- {
- uint8_t extruder;
- if (setTargetedHotend(221, extruder))
- break;
- extruder_multiply[extruder] = tmp_code;
- }
- else
- {
- extrudemultiply = tmp_code ;
- }
- }
- else
- {
- printf_P(PSTR("%i%%\n"), extrudemultiply);
- }
- calculate_extruder_multipliers();
- }
- break;
-
- case 226:
- {
- if(code_seen('P')){
- int pin_number = code_value_short();
- int pin_state = -1;
- if(code_seen('S')) pin_state = code_value_short();
- if(pin_state >= -1 && pin_state <= 1){
- for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(sensitive_pins[0])); i++)
- {
- if (((int8_t)pgm_read_byte(&sensitive_pins[i]) == pin_number))
- {
- pin_number = -1;
- break;
- }
- }
- if (pin_number > -1)
- {
- int target = LOW;
- st_synchronize();
- pinMode(pin_number, INPUT);
- switch(pin_state){
- case 1:
- target = HIGH;
- break;
- case 0:
- target = LOW;
- break;
- case -1:
- target = !digitalRead(pin_number);
- break;
- }
- while(digitalRead(pin_number) != target){
- manage_heater();
- manage_inactivity();
- lcd_update(0);
- }
- }
- }
- }
- }
- break;
- #if NUM_SERVOS > 0
-
- case 280:
- {
- int servo_index = -1;
- int servo_position = 0;
- if (code_seen('P'))
- servo_index = code_value();
- if (code_seen('S')) {
- servo_position = code_value();
- if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
- #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
- servos[servo_index].attach(0);
- #endif
- servos[servo_index].write(servo_position);
- #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
- _delay(PROBE_SERVO_DEACTIVATION_DELAY);
- servos[servo_index].detach();
- #endif
- }
- else {
- SERIAL_ECHO_START;
- SERIAL_ECHO("Servo ");
- SERIAL_ECHO(servo_index);
- SERIAL_ECHOLN(" out of range");
- }
- }
- else if (servo_index >= 0) {
- SERIAL_PROTOCOL(MSG_OK);
- SERIAL_PROTOCOL(" Servo ");
- SERIAL_PROTOCOL(servo_index);
- SERIAL_PROTOCOL(": ");
- SERIAL_PROTOCOLLN(servos[servo_index].read());
- }
- }
- break;
- #endif
- #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
-
-
- case 300:
- {
- uint16_t beepS = code_seen('S') ? code_value() : 0;
- uint16_t beepP = code_seen('P') ? code_value() : 1000;
- #if BEEPER > 0
- if (beepP > 0)
- Sound_MakeCustom(beepP,beepS,false);
- #endif
- }
- break;
- #endif
- #ifdef PIDTEMP
-
- case 301:
- {
- if(code_seen('P')) cs.Kp = code_value();
- if(code_seen('I')) cs.Ki = scalePID_i(code_value());
- if(code_seen('D')) cs.Kd = scalePID_d(code_value());
- updatePID();
- SERIAL_PROTOCOLRPGM(MSG_OK);
- SERIAL_PROTOCOLPGM(" p:");
- SERIAL_PROTOCOL(cs.Kp);
- SERIAL_PROTOCOLPGM(" i:");
- SERIAL_PROTOCOL(unscalePID_i(cs.Ki));
- SERIAL_PROTOCOLPGM(" d:");
- SERIAL_PROTOCOL(unscalePID_d(cs.Kd));
- SERIAL_PROTOCOLLN();
- }
- break;
- #endif
- #ifdef PIDTEMPBED
-
- case 304:
- {
- if(code_seen('P')) cs.bedKp = code_value();
- if(code_seen('I')) cs.bedKi = scalePID_i(code_value());
- if(code_seen('D')) cs.bedKd = scalePID_d(code_value());
- updatePID();
- SERIAL_PROTOCOLRPGM(MSG_OK);
- SERIAL_PROTOCOLPGM(" p:");
- SERIAL_PROTOCOL(cs.bedKp);
- SERIAL_PROTOCOLPGM(" i:");
- SERIAL_PROTOCOL(unscalePID_i(cs.bedKi));
- SERIAL_PROTOCOLPGM(" d:");
- SERIAL_PROTOCOLLN(unscalePID_d(cs.bedKd));
- }
- break;
- #endif
-
- case 240:
- {
- #ifdef CHDK
-
- SET_OUTPUT(CHDK);
- WRITE(CHDK, HIGH);
- chdkHigh = _millis();
- chdkActive = true;
-
- #else
-
- #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
- const uint8_t NUM_PULSES=16;
- const float PULSE_LENGTH=0.01524;
- for(int i=0; i < NUM_PULSES; i++) {
- WRITE(PHOTOGRAPH_PIN, HIGH);
- _delay_ms(PULSE_LENGTH);
- WRITE(PHOTOGRAPH_PIN, LOW);
- _delay_ms(PULSE_LENGTH);
- }
- _delay(7.33);
- for(int i=0; i < NUM_PULSES; i++) {
- WRITE(PHOTOGRAPH_PIN, HIGH);
- _delay_ms(PULSE_LENGTH);
- WRITE(PHOTOGRAPH_PIN, LOW);
- _delay_ms(PULSE_LENGTH);
- }
- #endif
- #endif
- }
- break;
- #ifdef PREVENT_DANGEROUS_EXTRUDE
-
- case 302:
- {
- int temp = 0;
- if (code_seen('S')) temp=code_value_short();
- set_extrude_min_temp(temp);
- }
- break;
- #endif
-
- case 303:
- {
- float temp = 150.0;
- int e = 0;
- int c = 5;
- if (code_seen('E')) e = code_value_short();
- if (e < 0)
- temp = 70;
- if (code_seen('S')) temp = code_value();
- if (code_seen('C')) c = code_value_short();
- PID_autotune(temp, e, c);
- }
- break;
-
-
- case 400:
- {
- st_synchronize();
- }
- break;
-
- case 403:
- {
-
-
-
- if (mmu_enabled)
- {
- uint8_t extruder = 255;
- uint8_t filament = FILAMENT_UNDEFINED;
- if(code_seen('E')) extruder = code_value_uint8();
- if(code_seen('F')) filament = code_value_uint8();
- mmu_set_filament_type(extruder, filament);
- }
- }
- break;
-
- case 500:
- {
- Config_StoreSettings();
- }
- break;
-
- case 501:
- {
- Config_RetrieveSettings();
- }
- break;
-
- case 502:
- {
- Config_ResetDefault();
- }
- break;
-
- case 503:
- {
- Config_PrintSettings();
- }
- break;
-
- case 509:
- {
- lang_reset();
- SERIAL_ECHO_START;
- SERIAL_PROTOCOLPGM(("LANG SEL FORCED"));
- }
- break;
- #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
-
- case 540:
- {
- if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
- }
- break;
- #endif
-
- #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
- case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
- {
- float value;
- if (code_seen('Z'))
- {
- value = code_value();
- if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
- {
- cs.zprobe_zoffset = -value;
- SERIAL_ECHO_START;
- SERIAL_ECHOLNRPGM(CAT4(MSG_ZPROBE_ZOFFSET, " ", MSG_OK,PSTR("")));
- SERIAL_PROTOCOLLN();
- }
- else
- {
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(MSG_ZPROBE_ZOFFSET);
- SERIAL_ECHORPGM(MSG_Z_MIN);
- SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
- SERIAL_ECHORPGM(MSG_Z_MAX);
- SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
- SERIAL_PROTOCOLLN();
- }
- }
- else
- {
- SERIAL_ECHO_START;
- SERIAL_ECHOLNRPGM(CAT2(MSG_ZPROBE_ZOFFSET, PSTR(" : ")));
- SERIAL_ECHO(-cs.zprobe_zoffset);
- SERIAL_PROTOCOLLN();
- }
- break;
- }
- #endif
-
- case 552:
- {
- if (code_seen('P'))
- {
- uint8_t valCnt = 0;
- IP_address = 0;
- do
- {
- *strchr_pointer = '*';
- ((uint8_t*)&IP_address)[valCnt] = code_value_short();
- valCnt++;
- } while ((valCnt < 4) && code_seen('.'));
-
- if (valCnt != 4)
- IP_address = 0;
- }
- } break;
- #ifdef FILAMENTCHANGEENABLE
-
- case 600:
- {
- st_synchronize();
- float x_position = current_position[X_AXIS];
- float y_position = current_position[Y_AXIS];
- float z_shift = 0;
- float e_shift_init = 0;
- float e_shift_late = 0;
- bool automatic = false;
-
-
- if(code_seen('E'))
- {
- e_shift_init = code_value();
- }
- else
- {
- #ifdef FILAMENTCHANGE_FIRSTRETRACT
- e_shift_init = FILAMENTCHANGE_FIRSTRETRACT ;
- #endif
- }
-
- if (code_seen('L'))
- {
- e_shift_late = code_value();
- }
- else
- {
- #ifdef FILAMENTCHANGE_FINALRETRACT
- e_shift_late = FILAMENTCHANGE_FINALRETRACT;
- #endif
- }
-
- if(code_seen('Z'))
- {
- z_shift = code_value();
- }
- else
- {
- z_shift = gcode_M600_filament_change_z_shift<uint8_t>();
- }
-
- if(code_seen('X'))
- {
- x_position = code_value();
- }
- else
- {
- #ifdef FILAMENTCHANGE_XPOS
- x_position = FILAMENTCHANGE_XPOS;
- #endif
- }
- if(code_seen('Y'))
- {
- y_position = code_value();
- }
- else
- {
- #ifdef FILAMENTCHANGE_YPOS
- y_position = FILAMENTCHANGE_YPOS ;
- #endif
- }
- if (mmu_enabled && code_seen_P(PSTR("AUTO")))
- automatic = true;
- gcode_M600(automatic, x_position, y_position, z_shift, e_shift_init, e_shift_late);
-
- }
- break;
- #endif
-
-
-
- case 25:
- case 601:
- {
- if (!isPrintPaused) {
- st_synchronize();
- ClearToSend();
- cmdqueue_pop_front();
- lcd_pause_print();
- }
- }
- break;
-
- case 602:
- {
- if (isPrintPaused) lcd_resume_print();
- }
- break;
-
- case 603: {
- lcd_print_stop();
- }
- break;
- #ifdef PINDA_THERMISTOR
-
- case 860:
- {
- int set_target_pinda = 0;
- if (code_seen('S')) {
- set_target_pinda = code_value_short();
- }
- else {
- break;
- }
- LCD_MESSAGERPGM(_T(MSG_PLEASE_WAIT));
- SERIAL_PROTOCOLPGM("Wait for PINDA target temperature:");
- SERIAL_PROTOCOLLN(set_target_pinda);
- codenum = _millis();
- cancel_heatup = false;
- bool is_pinda_cooling = false;
- if ((degTargetBed() == 0) && (degTargetHotend(0) == 0)) {
- is_pinda_cooling = true;
- }
- while ( ((!is_pinda_cooling) && (!cancel_heatup) && (current_temperature_pinda < set_target_pinda)) || (is_pinda_cooling && (current_temperature_pinda > set_target_pinda)) ) {
- if ((_millis() - codenum) > 1000)
- {
- SERIAL_PROTOCOLPGM("P:");
- SERIAL_PROTOCOL_F(current_temperature_pinda, 1);
- SERIAL_PROTOCOL('/');
- SERIAL_PROTOCOLLN(set_target_pinda);
- codenum = _millis();
- }
- manage_heater();
- manage_inactivity();
- lcd_update(0);
- }
- LCD_MESSAGERPGM(MSG_OK);
- break;
- }
-
-
- case 861: {
- const char * const _header = PSTR("index, temp, ustep, um");
- if (code_seen('?')) {
- int16_t usteps = 0;
- SERIAL_PROTOCOLPGM("PINDA cal status: ");
- SERIAL_PROTOCOLLN(calibration_status_pinda());
- SERIAL_PROTOCOLLNRPGM(_header);
- for (uint8_t i = 0; i < 6; i++)
- {
- if(i > 0) {
- usteps = eeprom_read_word((uint16_t*) EEPROM_PROBE_TEMP_SHIFT + (i - 1));
- }
- float mm = ((float)usteps) / cs.axis_steps_per_unit[Z_AXIS];
- i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1);
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOL(35 + (i * 5));
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOL(usteps);
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOLLN(mm * 1000);
- }
- }
- else if (code_seen('!')) {
- eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
- int16_t z_shift = 8;
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT, z_shift);
- z_shift = 24;
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 1, z_shift);
- z_shift = 48;
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 2, z_shift);
- z_shift = 80;
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 3, z_shift);
- z_shift = 120;
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 4, z_shift);
- SERIAL_PROTOCOLLNPGM("factory restored");
- }
- else if (code_seen('Z')) {
- eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
- int16_t z_shift = 0;
- for (uint8_t i = 0; i < 5; i++) {
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
- }
- SERIAL_PROTOCOLLNPGM("zerorized");
- }
- else if (code_seen('S')) {
- int16_t usteps = code_value_short();
- if (code_seen('I')) {
- uint8_t index = code_value_uint8();
- if (index < 5) {
- eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + index, usteps);
- SERIAL_PROTOCOLLNRPGM(MSG_OK);
- SERIAL_PROTOCOLLNRPGM(_header);
- for (uint8_t i = 0; i < 6; i++)
- {
- usteps = 0;
- if (i > 0) {
- usteps = eeprom_read_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + (i - 1));
- }
- float mm = ((float)usteps) / cs.axis_steps_per_unit[Z_AXIS];
- i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1);
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOL(35 + (i * 5));
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOL(usteps);
- SERIAL_PROTOCOLPGM(", ");
- SERIAL_PROTOCOLLN(mm * 1000);
- }
- }
- }
- }
- else {
- SERIAL_PROTOCOLLNPGM("no valid command");
- }
- } break;
- #endif
-
-
- case 862:
- float nDummy;
- uint8_t nCommand;
- nCommand=(uint8_t)(modff(code_value_float(),&nDummy)*10.0+0.5);
- switch((ClPrintChecking)nCommand)
- {
- case ClPrintChecking::_Nozzle:
- uint16_t nDiameter;
- if(code_seen('P'))
- {
- nDiameter=(uint16_t)(code_value()*1000.0+0.5);
- nozzle_diameter_check(nDiameter);
- }
- else if(code_seen('Q'))
- SERIAL_PROTOCOLLN((float)eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM)/1000.0);
- break;
- case ClPrintChecking::_Model:
- if(code_seen('P'))
- {
- uint16_t nPrinterModel;
- nPrinterModel=(uint16_t)code_value_long();
- printer_model_check(nPrinterModel);
- }
- else if(code_seen('Q'))
- SERIAL_PROTOCOLLN(nPrinterType);
- break;
- case ClPrintChecking::_Smodel:
- if(code_seen('P'))
- printer_smodel_check(strchr_pointer);
- else if(code_seen('Q'))
- SERIAL_PROTOCOLLNRPGM(sPrinterName);
- break;
- case ClPrintChecking::_Version:
- if(code_seen('P'))
- fw_version_check(++strchr_pointer);
- else if(code_seen('Q'))
- SERIAL_PROTOCOLLNRPGM(FW_VERSION_STR_P());
- break;
- case ClPrintChecking::_Gcode:
- if(code_seen('P'))
- {
- uint16_t nGcodeLevel;
- nGcodeLevel=(uint16_t)code_value_long();
- gcode_level_check(nGcodeLevel);
- }
- else if(code_seen('Q'))
- SERIAL_PROTOCOLLN(GCODE_LEVEL);
- break;
- }
- break;
- #ifdef LIN_ADVANCE
-
- case 900:
- gcode_M900();
- break;
- #endif
-
- case 907:
- {
- #ifdef TMC2130
-
- for (uint_least8_t i = 0; i < NUM_AXIS; i++){
- if(code_seen(axis_codes[i])){
- if( i == E_AXIS && FarmOrUserECool() ){
- SERIAL_ECHORPGM(eMotorCurrentScalingEnabled);
- SERIAL_ECHOLNPGM(", M907 E ignored");
- continue;
- }
- long cur_mA = code_value_long();
- uint8_t val = tmc2130_cur2val(cur_mA);
- tmc2130_set_current_h(i, val);
- tmc2130_set_current_r(i, val);
-
- }
- }
- #else
- #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
- for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) st_current_set(i,code_value());
- if(code_seen('B')) st_current_set(4,code_value());
- if(code_seen('S')) for(int i=0;i<=4;i++) st_current_set(i,code_value());
- #endif
- #ifdef MOTOR_CURRENT_PWM_XY_PIN
- if(code_seen('X')) st_current_set(0, code_value());
- #endif
- #ifdef MOTOR_CURRENT_PWM_Z_PIN
- if(code_seen('Z')) st_current_set(1, code_value());
- #endif
- #ifdef MOTOR_CURRENT_PWM_E_PIN
- if(code_seen('E')) st_current_set(2, code_value());
- #endif
- #endif
- }
- break;
-
- case 908:
- {
- #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
- uint8_t channel,current;
- if(code_seen('P')) channel=code_value();
- if(code_seen('S')) current=code_value();
- digitalPotWrite(channel, current);
- #endif
- }
- break;
- #ifdef TMC2130_SERVICE_CODES_M910_M918
-
- case 910:
- {
- tmc2130_init(TMCInitParams(false, FarmOrUserECool()));
- }
- break;
-
- case 911:
- {
- if (code_seen('X')) tmc2130_set_current_h(0, code_value());
- if (code_seen('Y')) tmc2130_set_current_h(1, code_value());
- if (code_seen('Z')) tmc2130_set_current_h(2, code_value());
- if (code_seen('E')) tmc2130_set_current_h(3, code_value());
- }
- break;
-
- case 912:
- {
- if (code_seen('X')) tmc2130_set_current_r(0, code_value());
- if (code_seen('Y')) tmc2130_set_current_r(1, code_value());
- if (code_seen('Z')) tmc2130_set_current_r(2, code_value());
- if (code_seen('E')) tmc2130_set_current_r(3, code_value());
- }
- break;
-
- case 913:
- {
- tmc2130_print_currents();
- }
- break;
-
- case 914:
- {
- tmc2130_mode = TMC2130_MODE_NORMAL;
- update_mode_profile();
- tmc2130_init(TMCInitParams(false, FarmOrUserECool()));
- }
- break;
-
- case 915:
- {
- tmc2130_mode = TMC2130_MODE_SILENT;
- update_mode_profile();
- tmc2130_init(TMCInitParams(false, FarmOrUserECool()));
- }
- break;
-
- case 916:
- {
- if (code_seen('X')) tmc2130_sg_thr[X_AXIS] = code_value();
- if (code_seen('Y')) tmc2130_sg_thr[Y_AXIS] = code_value();
- if (code_seen('Z')) tmc2130_sg_thr[Z_AXIS] = code_value();
- if (code_seen('E')) tmc2130_sg_thr[E_AXIS] = code_value();
- for (uint8_t a = X_AXIS; a <= E_AXIS; a++)
- printf_P(_N("tmc2130_sg_thr[%c]=%d\n"), "XYZE"[a], tmc2130_sg_thr[a]);
- }
- break;
-
- case 917:
- {
- if (code_seen('X')) tmc2130_set_pwm_ampl(0, code_value());
- if (code_seen('Y')) tmc2130_set_pwm_ampl(1, code_value());
- if (code_seen('Z')) tmc2130_set_pwm_ampl(2, code_value());
- if (code_seen('E')) tmc2130_set_pwm_ampl(3, code_value());
- }
- break;
-
- case 918:
- {
- if (code_seen('X')) tmc2130_set_pwm_grad(0, code_value());
- if (code_seen('Y')) tmc2130_set_pwm_grad(1, code_value());
- if (code_seen('Z')) tmc2130_set_pwm_grad(2, code_value());
- if (code_seen('E')) tmc2130_set_pwm_grad(3, code_value());
- }
- break;
- #endif
-
- case 350:
- {
- #ifdef TMC2130
- for (uint_least8_t i=0; i<NUM_AXIS; i++)
- {
- if(code_seen(axis_codes[i]))
- {
- uint16_t res_new = code_value();
- #ifdef ALLOW_ALL_MRES
- bool res_valid = res_new > 0 && res_new <= 256 && !(res_new & (res_new - 1));
- #else
- bool res_valid = (res_new == 8) || (res_new == 16) || (res_new == 32);
- res_valid |= (i != E_AXIS) && ((res_new == 1) || (res_new == 2) || (res_new == 4));
- res_valid |= (i == E_AXIS) && ((res_new == 64) || (res_new == 128));
- #endif
- if (res_valid)
- {
- st_synchronize();
- uint16_t res = tmc2130_get_res(i);
- tmc2130_set_res(i, res_new);
- cs.axis_ustep_resolution[i] = res_new;
- if (res_new > res)
- {
- uint16_t fac = (res_new / res);
- cs.axis_steps_per_unit[i] *= fac;
- position[i] *= fac;
- }
- else
- {
- uint16_t fac = (res / res_new);
- cs.axis_steps_per_unit[i] /= fac;
- position[i] /= fac;
- }
- #if defined(FILAMENT_SENSOR) && defined(PAT9125)
- if (i == E_AXIS)
- fsensor_set_axis_steps_per_unit(cs.axis_steps_per_unit[i]);
- #endif
- }
- }
- }
- reset_acceleration_rates();
- #else
- #if defined(X_MS1_PIN) && X_MS1_PIN > -1
- if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
- for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
- if(code_seen('B')) microstep_mode(4,code_value());
- microstep_readings();
- #endif
- #endif
- }
- break;
-
- case 351:
- {
- #if defined(X_MS1_PIN) && X_MS1_PIN > -1
- if(code_seen('S')) switch((int)code_value())
- {
- case 1:
- for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
- if(code_seen('B')) microstep_ms(4,code_value(),-1);
- break;
- case 2:
- for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
- if(code_seen('B')) microstep_ms(4,-1,code_value());
- break;
- }
- microstep_readings();
- #endif
- }
- break;
-
- case 701:
- {
- if (mmu_enabled && (code_seen('E') || code_seen('T')))
- tmp_extruder = code_value_uint8();
- gcode_M701();
- }
- break;
-
- case 702:
- {
- if (code_seen('C')) {
- if(mmu_enabled) extr_unload();
- }
- else {
- if(mmu_enabled) extr_unload();
- else unload_filament();
- }
- }
- break;
-
- case 999:
- Stopped = false;
- lcd_reset_alert_level();
- gcode_LastN = Stopped_gcode_LastN;
- FlushSerialRequestResend();
- break;
-
- default:
- printf_P(MSG_UNKNOWN_CODE, 'M', cmdbuffer + bufindr + CMDHDRSIZE);
- }
- mcode_in_progress = 0;
- }
- }
-
-
- else if(code_seen('T'))
- {
- static const char duplicate_Tcode_ignored[] PROGMEM = "Duplicate T-code ignored.";
-
- int index;
- bool load_to_nozzle = false;
- for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++);
- *(strchr_pointer + index) = tolower(*(strchr_pointer + index));
- if ((*(strchr_pointer + index) < '0' || *(strchr_pointer + index) > '4') && *(strchr_pointer + index) != '?' && *(strchr_pointer + index) != 'x' && *(strchr_pointer + index) != 'c') {
- SERIAL_ECHOLNPGM("Invalid T code.");
- }
- else if (*(strchr_pointer + index) == 'x'){
- if (mmu_enabled)
- {
- tmp_extruder = choose_menu_P(_T(MSG_SELECT_FILAMENT), _T(MSG_FILAMENT));
- if ((tmp_extruder == mmu_extruder) && mmu_fil_loaded)
- {
- puts_P(duplicate_Tcode_ignored);
- }
- else
- {
- st_synchronize();
- mmu_command(MmuCmd::T0 + tmp_extruder);
- manage_response(true, true, MMU_TCODE_MOVE);
- }
- }
- }
- else if (*(strchr_pointer + index) == 'c') {
- if (mmu_enabled)
- {
- st_synchronize();
- mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
- mmu_extruder = tmp_extruder;
- mmu_load_to_nozzle();
- }
- }
- else {
- if (*(strchr_pointer + index) == '?')
- {
- if(mmu_enabled)
- {
- tmp_extruder = choose_menu_P(_T(MSG_SELECT_FILAMENT), _T(MSG_FILAMENT));
- load_to_nozzle = true;
- } else
- {
- tmp_extruder = choose_menu_P(_T(MSG_SELECT_EXTRUDER), _T(MSG_EXTRUDER));
- }
- }
- else {
- tmp_extruder = code_value();
- if (mmu_enabled && lcd_autoDepleteEnabled())
- {
- tmp_extruder = ad_getAlternative(tmp_extruder);
- }
- }
- st_synchronize();
- if (mmu_enabled)
- {
- if ((tmp_extruder == mmu_extruder) && mmu_fil_loaded)
- {
- puts_P(duplicate_Tcode_ignored);
- }
- else
- {
- #if defined(MMU_HAS_CUTTER) && defined(MMU_ALWAYS_CUT)
- if (EEPROM_MMU_CUTTER_ENABLED_always == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
- {
- mmu_command(MmuCmd::K0 + tmp_extruder);
- manage_response(true, true, MMU_UNLOAD_MOVE);
- }
- #endif
- mmu_command(MmuCmd::T0 + tmp_extruder);
- manage_response(true, true, MMU_TCODE_MOVE);
- mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
- mmu_extruder = tmp_extruder;
- if (load_to_nozzle)
- {
- mmu_load_to_nozzle();
- }
- }
- }
- else
- {
- if (tmp_extruder >= EXTRUDERS) {
- SERIAL_ECHO_START;
- SERIAL_ECHO('T');
- SERIAL_PROTOCOLLN((int)tmp_extruder);
- SERIAL_ECHOLNRPGM(_n("Invalid extruder"));
- }
- else {
- #if EXTRUDERS > 1
- bool make_move = false;
- #endif
- if (code_seen('F')) {
- #if EXTRUDERS > 1
- make_move = true;
- #endif
- next_feedrate = code_value();
- if (next_feedrate > 0.0) {
- feedrate = next_feedrate;
- }
- }
- #if EXTRUDERS > 1
- if (tmp_extruder != active_extruder) {
-
- set_destination_to_current();
-
- int i;
- for (i = 0; i < 2; i++) {
- current_position[i] = current_position[i] -
- extruder_offset[i][active_extruder] +
- extruder_offset[i][tmp_extruder];
- }
-
- active_extruder = tmp_extruder;
- plan_set_position_curposXYZE();
-
- if (make_move && Stopped == false) {
- prepare_move();
- }
- }
- #endif
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(_n("Active Extruder: "));
- SERIAL_PROTOCOLLN((int)active_extruder);
- }
- }
- }
- }
-
-
- else if (code_seen('D'))
- {
- switch(code_value_short())
- {
-
- case -1:
- dcode__1(); break;
- #ifdef DEBUG_DCODES
-
- case 0:
- dcode_0(); break;
-
- case 1:
- dcode_1(); break;
- #endif
- #if defined DEBUG_DCODE2 || defined DEBUG_DCODES
-
- case 2:
- dcode_2(); break;
- #endif
- #if defined DEBUG_DCODE3 || defined DEBUG_DCODES
-
- case 3:
- dcode_3(); break;
- #endif
- #ifdef DEBUG_DCODES
-
- case 4:
- dcode_4(); break;
- #endif
- #if defined DEBUG_DCODE5 || defined DEBUG_DCODES
-
- case 5:
- dcode_5(); break;
- #endif
- #if defined DEBUG_DCODE6 || defined DEBUG_DCODES
-
- case 6:
- dcode_6(); break;
- #endif
- #ifdef DEBUG_DCODES
-
- case 7:
- dcode_7(); break;
-
- case 8:
- dcode_8(); break;
-
- case 9:
- dcode_9(); break;
-
- case 10:
- dcode_10(); break;
-
- #endif
- #ifdef XFLASH_DUMP
-
- case 20: {
- dcode_20();
- break;
- };
-
- case 21: {
- dcode_21();
- break;
- };
-
- case 22: {
- dcode_22();
- break;
- };
- #endif
- #ifdef EMERGENCY_SERIAL_DUMP
-
- case 23: {
- dcode_23();
- break;
- };
- #endif
- #ifdef HEATBED_ANALYSIS
-
- case 80:
- dcode_80(); break;
-
- case 81:
- dcode_81(); break;
-
- #endif
- #ifdef DEBUG_DCODES
-
- case 106:
- dcode_106(); break;
- #ifdef TMC2130
-
- case 2130:
- dcode_2130(); break;
- #endif
- #if (defined (FILAMENT_SENSOR) && defined(PAT9125))
-
- case 9125:
- dcode_9125(); break;
- #endif
- #endif
- default:
- printf_P(MSG_UNKNOWN_CODE, 'D', cmdbuffer + bufindr + CMDHDRSIZE);
- }
- }
- else
- {
- SERIAL_ECHO_START;
- SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND);
- SERIAL_ECHO(CMDBUFFER_CURRENT_STRING);
- SERIAL_ECHOLNPGM("\"(2)");
- }
- KEEPALIVE_STATE(NOT_BUSY);
- ClearToSend();
- }
- void FlushSerialRequestResend()
- {
-
- MYSERIAL.flush();
- printf_P(_N("%S: %ld\n%S\n"), _n("Resend"), gcode_LastN + 1, MSG_OK);
- }
- void ClearToSend()
- {
- previous_millis_cmd.start();
- if (buflen && ((CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB) || (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR)))
- SERIAL_PROTOCOLLNRPGM(MSG_OK);
- }
- #if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
- void update_currents() {
- float current_high[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
- float current_low[3] = DEFAULT_PWM_MOTOR_CURRENT;
- float tmp_motor[3];
-
-
- if (destination[Z_AXIS] < Z_SILENT) {
-
- for (uint8_t i = 0; i < 3; i++) {
- st_current_set(i, current_low[i]);
-
- }
- }
- else if (destination[Z_AXIS] > Z_HIGH_POWER) {
-
- for (uint8_t i = 0; i < 3; i++) {
- st_current_set(i, current_high[i]);
-
- }
- }
- else {
- for (uint8_t i = 0; i < 3; i++) {
- float q = current_low[i] - Z_SILENT*((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT));
- tmp_motor[i] = ((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT))*destination[Z_AXIS] + q;
- st_current_set(i, tmp_motor[i]);
-
- }
- }
- }
- #endif
- void get_coordinates()
- {
- bool seen[4]={false,false,false,false};
- for(int8_t i=0; i < NUM_AXIS; i++) {
- if(code_seen(axis_codes[i]))
- {
- bool relative = axis_relative_modes & (1 << i);
- destination[i] = code_value();
- if (i == E_AXIS) {
- float emult = extruder_multiplier[active_extruder];
- if (emult != 1.) {
- if (! relative) {
- destination[i] -= current_position[i];
- relative = true;
- }
- destination[i] *= emult;
- }
- }
- if (relative)
- destination[i] += current_position[i];
- seen[i]=true;
- #if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3
- if (i == Z_AXIS && SilentModeMenu == SILENT_MODE_AUTO) update_currents();
- #endif
- }
- else destination[i] = current_position[i];
- }
- if(code_seen('F')) {
- next_feedrate = code_value();
- if(next_feedrate > 0.0) feedrate = next_feedrate;
- if (!seen[0] && !seen[1] && !seen[2] && seen[3])
- {
- }
- }
- }
- void get_arc_coordinates()
- {
- #ifdef SF_ARC_FIX
- bool relative_mode_backup = relative_mode;
- relative_mode = true;
- #endif
- get_coordinates();
- #ifdef SF_ARC_FIX
- relative_mode=relative_mode_backup;
- #endif
- if(code_seen('I')) {
- offset[0] = code_value();
- }
- else {
- offset[0] = 0.0;
- }
- if(code_seen('J')) {
- offset[1] = code_value();
- }
- else {
- offset[1] = 0.0;
- }
- }
- void clamp_to_software_endstops(float target[3])
- {
- #ifdef DEBUG_DISABLE_SWLIMITS
- return;
- #endif
- world2machine_clamp(target[0], target[1]);
-
- if (min_software_endstops) {
- float negative_z_offset = 0;
- #ifdef ENABLE_AUTO_BED_LEVELING
- if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER;
- if (cs.add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + cs.add_homing[Z_AXIS];
- #endif
- if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset;
- }
- if (max_software_endstops) {
- if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
- }
- }
- #ifdef MESH_BED_LEVELING
- void mesh_plan_buffer_line(const float &x, const float &y, const float &z, const float &e, const float &feed_rate, const uint8_t extruder) {
- float dx = x - current_position[X_AXIS];
- float dy = y - current_position[Y_AXIS];
- int n_segments = 0;
- if (mbl.active) {
- float len = fabs(dx) + fabs(dy);
- if (len > 0)
-
- n_segments = int(ceil(len / 30.f));
- }
- if (n_segments > 1) {
-
-
- float gcode_target[NUM_AXIS];
- gcode_target[X_AXIS] = x;
- gcode_target[Y_AXIS] = y;
- gcode_target[Z_AXIS] = z;
- gcode_target[E_AXIS] = e;
- float dz = z - current_position[Z_AXIS];
- float de = e - current_position[E_AXIS];
- for (int i = 1; i < n_segments; ++ i) {
- float t = float(i) / float(n_segments);
- plan_buffer_line(current_position[X_AXIS] + t * dx,
- current_position[Y_AXIS] + t * dy,
- current_position[Z_AXIS] + t * dz,
- current_position[E_AXIS] + t * de,
- feed_rate, extruder, gcode_target);
- if (waiting_inside_plan_buffer_line_print_aborted)
- return;
- }
- }
-
- plan_buffer_line(x, y, z, e, feed_rate, extruder);
- }
- #endif
-
- void prepare_move()
- {
- clamp_to_software_endstops(destination);
- previous_millis_cmd.start();
-
- if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
- plan_buffer_line_destinationXYZE(feedrate/60);
- }
- else {
- #ifdef MESH_BED_LEVELING
- mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder);
- #else
- plan_buffer_line_destinationXYZE(feedrate*feedmultiply*(1./(60.f*100.f)));
- #endif
- }
- set_current_to_destination();
- }
- void prepare_arc_move(bool isclockwise) {
- float r = hypot(offset[X_AXIS], offset[Y_AXIS]);
-
- mc_arc(current_position, destination, offset, feedrate * feedmultiply / 60 / 100.0, r, isclockwise, active_extruder);
-
-
-
- set_current_to_destination();
- previous_millis_cmd.start();
- }
- #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
- #if defined(FAN_PIN)
- #if CONTROLLERFAN_PIN == FAN_PIN
- #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
- #endif
- #endif
- unsigned long lastMotor = 0;
- unsigned long lastMotorCheck = 0;
- void controllerFan()
- {
- if ((_millis() - lastMotorCheck) >= 2500)
- {
- lastMotorCheck = _millis();
- if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
- #if EXTRUDERS > 2
- || !READ(E2_ENABLE_PIN)
- #endif
- #if EXTRUDER > 1
- #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
- || !READ(X2_ENABLE_PIN)
- #endif
- || !READ(E1_ENABLE_PIN)
- #endif
- || !READ(E0_ENABLE_PIN))
- {
- lastMotor = _millis();
- }
- if ((_millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0)
- {
- digitalWrite(CONTROLLERFAN_PIN, 0);
- analogWrite(CONTROLLERFAN_PIN, 0);
- }
- else
- {
-
- digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
- analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
- }
- }
- }
- #endif
- #ifdef SAFETYTIMER
- static void handleSafetyTimer()
- {
- #if (EXTRUDERS > 1)
- #error Implemented only for one extruder.
- #endif
- if ((PRINTER_ACTIVE) || (!degTargetBed() && !degTargetHotend(0)) || (!safetytimer_inactive_time))
- {
- safetyTimer.stop();
- }
- else if ((degTargetBed() || degTargetHotend(0)) && (!safetyTimer.running()))
- {
- safetyTimer.start();
- }
- else if (safetyTimer.expired(farm_mode?FARM_DEFAULT_SAFETYTIMER_TIME_ms:safetytimer_inactive_time))
- {
- setTargetBed(0);
- setAllTargetHotends(0);
- lcd_show_fullscreen_message_and_wait_P(_i("Heating disabled by safety timer."));
- }
- }
- #endif
- #ifdef IR_SENSOR_ANALOG
- #define FS_CHECK_COUNT 16
- void manage_inactivity_IR_ANALOG_Check(uint16_t &nFSCheckCount, ClFsensorPCB isVersion, ClFsensorPCB switchTo, const char *statusLineTxt_P) {
- bool bTemp = (!CHECK_ALL_HEATERS);
- bTemp = bTemp && (menu_menu == lcd_status_screen);
- bTemp = bTemp && ((oFsensorPCB == isVersion) || (oFsensorPCB == ClFsensorPCB::_Undef));
- bTemp = bTemp && fsensor_enabled;
- if (bTemp) {
- nFSCheckCount++;
- if (nFSCheckCount > FS_CHECK_COUNT) {
- nFSCheckCount = 0;
- oFsensorPCB = switchTo;
- eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_PCB, (uint8_t)oFsensorPCB);
- printf_IRSensorAnalogBoardChange();
- lcd_setstatuspgm(statusLineTxt_P);
- }
- } else {
- nFSCheckCount = 0;
- }
- }
- #endif
- void manage_inactivity(bool ignore_stepper_queue)
- {
- #ifdef FILAMENT_SENSOR
- bool bInhibitFlag = false;
- #ifdef IR_SENSOR_ANALOG
- static uint16_t nFSCheckCount=0;
- #endif
- if (mmu_enabled == false)
- {
- if (!PRINTER_ACTIVE) bInhibitFlag=(menu_menu==lcd_menu_show_sensors_state);
- #ifdef IR_SENSOR_ANALOG
- bInhibitFlag=bInhibitFlag||bMenuFSDetect;
- #endif
- if ((mcode_in_progress != 600) && (eFilamentAction != FilamentAction::AutoLoad) && (!bInhibitFlag) && (menu_menu != lcd_move_e))
- {
- if (!moves_planned() && !IS_SD_PRINTING && !usb_timer.running() && (lcd_commands_type != LcdCommands::Layer1Cal) && ! eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE))
- {
- #ifdef IR_SENSOR_ANALOG
- static uint16_t minVolt = Voltage2Raw(6.F), maxVolt = 0;
-
-
- if( current_voltage_raw_IR > maxVolt )maxVolt = current_voltage_raw_IR;
- if( current_voltage_raw_IR < minVolt )minVolt = current_voltage_raw_IR;
-
- #if 0
- {
- static uint16_t lastVolt = ~0U;
- if( current_voltage_raw_IR != lastVolt ){
- printf_P(PSTR("fs volt=%4.2fV (min=%4.2f max=%4.2f)\n"), Raw2Voltage(current_voltage_raw_IR), Raw2Voltage(minVolt), Raw2Voltage(maxVolt) );
- lastVolt = current_voltage_raw_IR;
- }
- }
- #endif
-
-
-
-
-
-
-
-
-
-
- if( minVolt >= IRsensor_Ldiode_TRESHOLD && minVolt <= IRsensor_Lmax_TRESHOLD
- && maxVolt >= IRsensor_Hmin_TRESHOLD && maxVolt <= IRsensor_Hopen_TRESHOLD
- ){
- manage_inactivity_IR_ANALOG_Check(nFSCheckCount, ClFsensorPCB::_Old, ClFsensorPCB::_Rev04, _i("FS v0.4 or newer") );
- }
-
-
-
- else if( minVolt < IRsensor_Ldiode_TRESHOLD
- && maxVolt > IRsensor_Hopen_TRESHOLD && maxVolt <= IRsensor_VMax_TRESHOLD
- ){
- manage_inactivity_IR_ANALOG_Check(nFSCheckCount, ClFsensorPCB::_Rev04, oFsensorPCB=ClFsensorPCB::_Old, _i("FS v0.3 or older"));
- }
- #endif
- if (fsensor_check_autoload())
- {
- #ifdef PAT9125
- fsensor_autoload_check_stop();
- #endif
- if(0)
- {
- Sound_MakeCustom(50,1000,false);
- loading_flag = true;
- enquecommand_front_P((PSTR("M701")));
- }
- else
- {
- eFilamentAction=FilamentAction::AutoLoad;
- bFilamentFirstRun=false;
- if(target_temperature[0] >= extrude_min_temp){
- bFilamentPreheatState=true;
- menu_submenu(mFilamentItemForce);
- } else {
- menu_submenu(lcd_generic_preheat_menu);
- lcd_timeoutToStatus.start();
- }
- }
- }
- }
- else
- {
- #ifdef PAT9125
- fsensor_autoload_check_stop();
- #endif
- if (fsensor_enabled && !saved_printing)
- fsensor_update();
- }
- }
- }
- #endif
- #ifdef SAFETYTIMER
- handleSafetyTimer();
- #endif
- #if defined(KILL_PIN) && KILL_PIN > -1
- static int killCount = 0;
- const int KILL_DELAY = 10000;
- #endif
-
- if(buflen < (BUFSIZE-1)){
- get_command();
- }
- if(previous_millis_cmd.expired(max_inactive_time))
- if(max_inactive_time)
- kill(_n("Inactivity Shutdown"), 4);
- if(stepper_inactive_time) {
- if(previous_millis_cmd.expired(stepper_inactive_time))
- {
- if(blocks_queued() == false && ignore_stepper_queue == false) {
- disable_x();
- disable_y();
- disable_z();
- disable_e0();
- disable_e1();
- disable_e2();
- }
- }
- }
-
- #ifdef CHDK
- if (chdkActive && (_millis() - chdkHigh > CHDK_DELAY))
- {
- chdkActive = false;
- WRITE(CHDK, LOW);
- }
- #endif
-
- #if defined(KILL_PIN) && KILL_PIN > -1
-
-
-
-
- if( 0 == READ(KILL_PIN) )
- {
- killCount++;
- }
- else if (killCount > 0)
- {
- killCount--;
- }
-
-
-
- if ( killCount >= KILL_DELAY)
- {
- kill(NULL, 5);
- }
- #endif
-
- #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
- controllerFan();
- #endif
- #ifdef EXTRUDER_RUNOUT_PREVENT
- if(previous_millis_cmd.expired(EXTRUDER_RUNOUT_SECONDS*1000))
- if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
- {
- bool oldstatus=READ(E0_ENABLE_PIN);
- enable_e0();
- float oldepos=current_position[E_AXIS];
- float oldedes=destination[E_AXIS];
- plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
- destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/cs.axis_steps_per_unit[E_AXIS],
- EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/cs.axis_steps_per_unit[E_AXIS], active_extruder);
- current_position[E_AXIS]=oldepos;
- destination[E_AXIS]=oldedes;
- plan_set_e_position(oldepos);
- previous_millis_cmd.start();
- st_synchronize();
- WRITE(E0_ENABLE_PIN,oldstatus);
- }
- #endif
- check_axes_activity();
- mmu_loop();
-
- if(lcd_longpress_trigger)
- {
-
- if (lcd_longpress_func && lcd_update_enabled)
- {
- lcd_longpress_func();
- lcd_longpress_trigger = 0;
- }
- }
- #if defined(AUTO_REPORT)
- host_autoreport();
- #endif
- host_keepalive();
- }
- void kill(const char *full_screen_message, unsigned char id)
- {
- printf_P(_N("KILL: %d\n"), id);
-
- cli();
- disable_heater();
- disable_x();
- disable_y();
- poweroff_z();
- disable_e0();
- disable_e1();
- disable_e2();
- #if defined(PS_ON_PIN) && PS_ON_PIN > -1
- pinMode(PS_ON_PIN,INPUT);
- #endif
- SERIAL_ERROR_START;
- SERIAL_ERRORLNRPGM(_n("Printer halted. kill() called!"));
- if (full_screen_message != NULL) {
- SERIAL_ERRORLNRPGM(full_screen_message);
- lcd_display_message_fullscreen_P(full_screen_message);
- } else {
- LCD_ALERTMESSAGERPGM(_n("KILLED. "));
- }
-
- sei();
- for ( int i=5; i--; lcd_update(0))
- {
- _delay(200);
- }
- cli();
- suicide();
- while(1)
- {
- #ifdef WATCHDOG
- wdt_reset();
- #endif
-
-
- }
- }
- void UnconditionalStop()
- {
- CRITICAL_SECTION_START;
-
- disable_heater();
- cancel_heatup = true;
-
- cancel_saved_printing();
-
- planner_abort_hard();
-
- cmdqueue_reset();
- cmdqueue_serial_disabled = false;
-
- card.sdprinting = false;
- card.closefile();
- st_reset_timer();
- CRITICAL_SECTION_END;
- }
- void Stop()
- {
-
- disable_heater();
-
- if(Stopped == false) {
- Stopped = true;
- lcd_print_stop();
- Stopped_gcode_LastN = gcode_LastN;
-
-
- SERIAL_ERROR_START;
- SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
- LCD_MESSAGERPGM(_T(MSG_STOPPED));
- }
-
-
-
- if (menu_menu != lcd_status_screen)
- lcd_return_to_status();
- }
- bool IsStopped() { return Stopped; };
- void finishAndDisableSteppers()
- {
- st_synchronize();
- disable_x();
- disable_y();
- disable_z();
- disable_e0();
- disable_e1();
- disable_e2();
- #ifndef LA_NOCOMPAT
-
-
-
- la10c_reset();
- #endif
-
- print_time_remaining_init();
- }
- #ifdef FAST_PWM_FAN
- void setPwmFrequency(uint8_t pin, int val)
- {
- val &= 0x07;
- switch(digitalPinToTimer(pin))
- {
- #if defined(TCCR0A)
- case TIMER0A:
- case TIMER0B:
- break;
- #endif
- #if defined(TCCR1A)
- case TIMER1A:
- case TIMER1B:
- break;
- #endif
- #if defined(TCCR2)
- case TIMER2:
- case TIMER2:
- TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
- TCCR2 |= val;
- break;
- #endif
- #if defined(TCCR2A)
- case TIMER2A:
- case TIMER2B:
- TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
- TCCR2B |= val;
- break;
- #endif
- #if defined(TCCR3A)
- case TIMER3A:
- case TIMER3B:
- case TIMER3C:
- TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
- TCCR3B |= val;
- break;
- #endif
- #if defined(TCCR4A)
- case TIMER4A:
- case TIMER4B:
- case TIMER4C:
- TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
- TCCR4B |= val;
- break;
- #endif
- #if defined(TCCR5A)
- case TIMER5A:
- case TIMER5B:
- case TIMER5C:
- TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
- TCCR5B |= val;
- break;
- #endif
- }
- }
- #endif
- bool setTargetedHotend(int code, uint8_t &extruder)
- {
- extruder = active_extruder;
- if(code_seen('T')) {
- extruder = code_value_uint8();
- if(extruder >= EXTRUDERS) {
- SERIAL_ECHO_START;
- switch(code){
- case 104:
- SERIAL_ECHORPGM(_n("M104 Invalid extruder "));
- break;
- case 105:
- SERIAL_ECHORPGM(_n("M105 Invalid extruder "));
- break;
- case 109:
- SERIAL_ECHORPGM(_n("M109 Invalid extruder "));
- break;
- case 218:
- SERIAL_ECHORPGM(_n("M218 Invalid extruder "));
- break;
- case 221:
- SERIAL_ECHORPGM(_n("M221 Invalid extruder "));
- break;
- }
- SERIAL_PROTOCOLLN((int)extruder);
- return true;
- }
- }
- return false;
- }
- void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time)
- {
- if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255)
- {
- eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
- eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
- }
- unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED);
- unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME);
- eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60));
- eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000));
- total_filament_used = 0;
- }
- float calculate_extruder_multiplier(float diameter) {
- float out = 1.f;
- if (cs.volumetric_enabled && diameter > 0.f) {
- float area = M_PI * diameter * diameter * 0.25;
- out = 1.f / area;
- }
- if (extrudemultiply != 100)
- out *= float(extrudemultiply) * 0.01f;
- return out;
- }
- void calculate_extruder_multipliers() {
- extruder_multiplier[0] = calculate_extruder_multiplier(cs.filament_size[0]);
- #if EXTRUDERS > 1
- extruder_multiplier[1] = calculate_extruder_multiplier(cs.filament_size[1]);
- #if EXTRUDERS > 2
- extruder_multiplier[2] = calculate_extruder_multiplier(cs.filament_size[2]);
- #endif
- #endif
- }
- void delay_keep_alive(unsigned int ms)
- {
- for (;;) {
- manage_heater();
-
- manage_inactivity(true);
- lcd_update(0);
- if (ms == 0)
- break;
- else if (ms >= 50) {
- _delay(50);
- ms -= 50;
- } else {
- _delay(ms);
- ms = 0;
- }
- }
- }
- static void wait_for_heater(long codenum, uint8_t extruder) {
- if (!degTargetHotend(extruder))
- return;
- #ifdef TEMP_RESIDENCY_TIME
- long residencyStart;
- residencyStart = -1;
-
- cancel_heatup = false;
- while ((!cancel_heatup) && ((residencyStart == -1) ||
- (residencyStart >= 0 && (((unsigned int)(_millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))))) {
- #else
- while (target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder) && (CooldownNoWait == false))) {
- #endif
- if ((_millis() - codenum) > 1000UL)
- {
- if (!farm_mode) {
- SERIAL_PROTOCOLPGM("T:");
- SERIAL_PROTOCOL_F(degHotend(extruder), 1);
- SERIAL_PROTOCOLPGM(" E:");
- SERIAL_PROTOCOL((int)extruder);
- #ifdef TEMP_RESIDENCY_TIME
- SERIAL_PROTOCOLPGM(" W:");
- if (residencyStart > -1)
- {
- codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (_millis() - residencyStart)) / 1000UL;
- SERIAL_PROTOCOLLN(codenum);
- }
- else
- {
- SERIAL_PROTOCOLLN('?');
- }
- }
- #else
- SERIAL_PROTOCOLLN();
- #endif
- codenum = _millis();
- }
- manage_heater();
- manage_inactivity(true);
- lcd_update(0);
- #ifdef TEMP_RESIDENCY_TIME
-
- if ((residencyStart == -1 && target_direction && (degHotend(extruder) >= (degTargetHotend(extruder) - TEMP_WINDOW))) ||
- (residencyStart == -1 && !target_direction && (degHotend(extruder) <= (degTargetHotend(extruder) + TEMP_WINDOW))) ||
- (residencyStart > -1 && labs(degHotend(extruder) - degTargetHotend(extruder)) > TEMP_HYSTERESIS))
- {
- residencyStart = _millis();
- }
- #endif
- }
- }
- void check_babystep()
- {
- int babystep_z = eeprom_read_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->
- s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)));
- if ((babystep_z < Z_BABYSTEP_MIN) || (babystep_z > Z_BABYSTEP_MAX)) {
- babystep_z = 0;
- SERIAL_ECHOLNPGM("Z live adjust out of range. Setting to 0");
- eeprom_write_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->
- s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),
- babystep_z);
- lcd_show_fullscreen_message_and_wait_P(PSTR("Z live adjust out of range. Setting to 0. Click to continue."));
- lcd_update_enable(true);
- }
- }
- #ifdef HEATBED_ANALYSIS
- void d_setup()
- {
- pinMode(D_DATACLOCK, INPUT_PULLUP);
- pinMode(D_DATA, INPUT_PULLUP);
- pinMode(D_REQUIRE, OUTPUT);
- digitalWrite(D_REQUIRE, HIGH);
- }
- float d_ReadData()
- {
- int digit[13];
- String mergeOutput;
- float output;
- digitalWrite(D_REQUIRE, HIGH);
- for (int i = 0; i<13; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- while (digitalRead(D_DATACLOCK) == LOW) {}
- while (digitalRead(D_DATACLOCK) == HIGH) {}
- bitWrite(digit[i], j, digitalRead(D_DATA));
- }
- }
- digitalWrite(D_REQUIRE, LOW);
- mergeOutput = "";
- output = 0;
- for (int r = 5; r <= 10; r++)
- {
- mergeOutput += digit[r];
- }
- output = mergeOutput.toFloat();
- if (digit[4] == 8)
- {
- output *= -1;
- }
- for (int i = digit[11]; i > 0; i--)
- {
- output /= 10;
- }
- return output;
- }
- void bed_check(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) {
- int t1 = 0;
- int t_delay = 0;
- int digit[13];
- int m;
- char str[3];
-
- char mergeOutput[15];
- float output;
- int mesh_point = 0;
- float bed_zero_ref_x = (-22.f + X_PROBE_OFFSET_FROM_EXTRUDER);
- float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER);
- float mesh_home_z_search = 4;
- float measure_z_height = 0.2f;
- float row[x_points_num];
- int ix = 0;
- int iy = 0;
- const char* filename_wldsd = "mesh.txt";
- char data_wldsd[x_points_num * 7 + 1];
- char numb_wldsd[8];
- #ifdef MICROMETER_LOGGING
- d_setup();
- #endif
- int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
- int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
- unsigned int custom_message_type_old = custom_message_type;
- unsigned int custom_message_state_old = custom_message_state;
- custom_message_type = CustomMsg::MeshBedLeveling;
- custom_message_state = (x_points_num * y_points_num) + 10;
- lcd_update(1);
-
- babystep_undo();
- card.openFile(filename_wldsd, false);
-
- destination[Z_AXIS] = measure_z_height;
- plan_buffer_line_destinationXYZE(Z_LIFT_FEEDRATE);
- for(int8_t i=0; i < NUM_AXIS; i++) {
- current_position[i] = destination[i];
- }
- st_synchronize();
- setup_for_endstop_move(false);
- SERIAL_PROTOCOLPGM("Num X,Y: ");
- SERIAL_PROTOCOL(x_points_num);
- SERIAL_PROTOCOLPGM(",");
- SERIAL_PROTOCOL(y_points_num);
- SERIAL_PROTOCOLPGM("\nZ search height: ");
- SERIAL_PROTOCOL(mesh_home_z_search);
- SERIAL_PROTOCOLPGM("\nDimension X,Y: ");
- SERIAL_PROTOCOL(x_dimension);
- SERIAL_PROTOCOLPGM(",");
- SERIAL_PROTOCOL(y_dimension);
- SERIAL_PROTOCOLLNPGM("\nMeasured points:");
- while (mesh_point != x_points_num * y_points_num) {
- ix = mesh_point % x_points_num;
- iy = mesh_point / x_points_num;
- if (iy & 1) ix = (x_points_num - 1) - ix;
- float z0 = 0.f;
-
-
-
- destination[X_AXIS] = ix * (x_dimension / (x_points_num - 1)) + shift_x;
- destination[Y_AXIS] = iy * (y_dimension / (y_points_num - 1)) + shift_y;
- mesh_plan_buffer_line_destinationXYZE(XY_AXIS_FEEDRATE/6);
- set_current_to_destination();
- st_synchronize();
-
- delay_keep_alive(1000);
- #ifdef MICROMETER_LOGGING
-
-
-
-
-
-
-
-
-
-
-
- memset(digit, 0, sizeof(digit));
-
- digitalWrite(D_REQUIRE, LOW);
-
- for (int i = 0; i<13; i++)
- {
-
- for (int j = 0; j < 4; j++)
- {
- while (digitalRead(D_DATACLOCK) == LOW) {}
- while (digitalRead(D_DATACLOCK) == HIGH) {}
-
- bitWrite(digit[i], j, digitalRead(D_DATA));
- }
-
-
-
-
- }
-
- digitalWrite(D_REQUIRE, HIGH);
- mergeOutput[0] = '\0';
- output = 0;
- for (int r = 5; r <= 10; r++)
- {
- sprintf(str, "%d", digit[r]);
- strcat(mergeOutput, str);
- }
-
- output = atof(mergeOutput);
- if (digit[4] == 8)
- {
- output *= -1;
- }
- for (int i = digit[11]; i > 0; i--)
- {
- output *= 0.1;
- }
-
-
-
-
-
-
- row[ix] = output;
- if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) {
- memset(data_wldsd, 0, sizeof(data_wldsd));
- for (int i = 0; i < x_points_num; i++) {
- SERIAL_PROTOCOLPGM(" ");
- SERIAL_PROTOCOL_F(row[i], 5);
- memset(numb_wldsd, 0, sizeof(numb_wldsd));
- dtostrf(row[i], 7, 3, numb_wldsd);
- strcat(data_wldsd, numb_wldsd);
- }
- card.write_command(data_wldsd);
- SERIAL_PROTOCOLPGM("\n");
- }
- custom_message_state--;
- mesh_point++;
- lcd_update(1);
- }
- #endif
- card.closefile();
-
- }
- void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) {
- int t1 = 0;
- int t_delay = 0;
- int digit[13];
- int m;
- char str[3];
-
- char mergeOutput[15];
- float output;
- int mesh_point = 0;
- float bed_zero_ref_x = (-22.f + X_PROBE_OFFSET_FROM_EXTRUDER);
- float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER);
- float mesh_home_z_search = 4;
- float row[x_points_num];
- int ix = 0;
- int iy = 0;
- const char* filename_wldsd = "wldsd.txt";
- char data_wldsd[70];
- char numb_wldsd[10];
- d_setup();
- if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) {
-
-
-
- repeatcommand_front();
-
- enquecommand_front_P(G28W0);
- enquecommand_front_P((PSTR("G1 Z5")));
- return;
- }
- unsigned int custom_message_type_old = custom_message_type;
- unsigned int custom_message_state_old = custom_message_state;
- custom_message_type = CustomMsg::MeshBedLeveling;
- custom_message_state = (x_points_num * y_points_num) + 10;
- lcd_update(1);
- mbl.reset();
- babystep_undo();
- card.openFile(filename_wldsd, false);
- current_position[Z_AXIS] = mesh_home_z_search;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 60, active_extruder);
- int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20;
- int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40;
- int l_feedmultiply = setup_for_endstop_move(false);
- SERIAL_PROTOCOLPGM("Num X,Y: ");
- SERIAL_PROTOCOL(x_points_num);
- SERIAL_PROTOCOLPGM(",");
- SERIAL_PROTOCOL(y_points_num);
- SERIAL_PROTOCOLPGM("\nZ search height: ");
- SERIAL_PROTOCOL(mesh_home_z_search);
- SERIAL_PROTOCOLPGM("\nDimension X,Y: ");
- SERIAL_PROTOCOL(x_dimension);
- SERIAL_PROTOCOLPGM(",");
- SERIAL_PROTOCOL(y_dimension);
- SERIAL_PROTOCOLLNPGM("\nMeasured points:");
- while (mesh_point != x_points_num * y_points_num) {
- ix = mesh_point % x_points_num;
- iy = mesh_point / x_points_num;
- if (iy & 1) ix = (x_points_num - 1) - ix;
- float z0 = 0.f;
- current_position[Z_AXIS] = mesh_home_z_search;
- plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder);
- st_synchronize();
- current_position[X_AXIS] = 13.f + ix * (x_dimension / (x_points_num - 1)) - bed_zero_ref_x + shift_x;
- current_position[Y_AXIS] = 6.4f + iy * (y_dimension / (y_points_num - 1)) - bed_zero_ref_y + shift_y;
- plan_buffer_line_curposXYZE(XY_AXIS_FEEDRATE, active_extruder);
- st_synchronize();
- if (!find_bed_induction_sensor_point_z(-10.f)) {
- break;
- card.closefile();
- }
-
-
-
-
-
-
-
-
-
-
-
- memset(digit, 0, sizeof(digit));
-
- digitalWrite(D_REQUIRE, LOW);
-
- for (int i = 0; i<13; i++)
- {
-
- for (int j = 0; j < 4; j++)
- {
- while (digitalRead(D_DATACLOCK) == LOW) {}
- while (digitalRead(D_DATACLOCK) == HIGH) {}
- bitWrite(digit[i], j, digitalRead(D_DATA));
- }
-
-
-
-
- }
-
- digitalWrite(D_REQUIRE, HIGH);
- mergeOutput[0] = '\0';
- output = 0;
- for (int r = 5; r <= 10; r++)
- {
- sprintf(str, "%d", digit[r]);
- strcat(mergeOutput, str);
- }
-
- output = atof(mergeOutput);
- if (digit[4] == 8)
- {
- output *= -1;
- }
- for (int i = digit[11]; i > 0; i--)
- {
- output *= 0.1;
- }
-
-
-
- memset(data_wldsd, 0, sizeof(data_wldsd));
- for (int i = 0; i <3; i++) {
- memset(numb_wldsd, 0, sizeof(numb_wldsd));
- dtostrf(current_position[i], 8, 5, numb_wldsd);
- strcat(data_wldsd, numb_wldsd);
- strcat(data_wldsd, ";");
- }
- memset(numb_wldsd, 0, sizeof(numb_wldsd));
- dtostrf(output, 8, 5, numb_wldsd);
- strcat(data_wldsd, numb_wldsd);
-
- card.write_command(data_wldsd);
-
-
-
- row[ix] = output;
- if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) {
- for (int i = 0; i < x_points_num; i++) {
- SERIAL_PROTOCOLPGM(" ");
- SERIAL_PROTOCOL_F(row[i], 5);
- }
- SERIAL_PROTOCOLPGM("\n");
- }
- custom_message_state--;
- mesh_point++;
- lcd_update(1);
- }
- card.closefile();
- clean_up_after_endstop_move(l_feedmultiply);
- }
- #endif
- #ifndef PINDA_THERMISTOR
- static void temp_compensation_start() {
-
- custom_message_type = CustomMsg::TempCompPreheat;
- custom_message_state = PINDA_HEAT_T + 1;
- lcd_update(2);
- if ((int)degHotend(active_extruder) > extrude_min_temp) {
- current_position[E_AXIS] -= default_retraction;
- }
- plan_buffer_line_curposXYZE(400, active_extruder);
-
- current_position[X_AXIS] = PINDA_PREHEAT_X;
- current_position[Y_AXIS] = PINDA_PREHEAT_Y;
- current_position[Z_AXIS] = PINDA_PREHEAT_Z;
- plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
- st_synchronize();
- while (fabs(degBed() - target_temperature_bed) > 1) delay_keep_alive(1000);
- for (int i = 0; i < PINDA_HEAT_T; i++) {
- delay_keep_alive(1000);
- custom_message_state = PINDA_HEAT_T - i;
- if (custom_message_state == 99 || custom_message_state == 9) lcd_update(2);
- else lcd_update(1);
- }
- custom_message_type = CustomMsg::Status;
- custom_message_state = 0;
- }
- static void temp_compensation_apply() {
- int i_add;
- int z_shift = 0;
- float z_shift_mm;
- if (calibration_status() == CALIBRATION_STATUS_CALIBRATED) {
- if (target_temperature_bed % 10 == 0 && target_temperature_bed >= 60 && target_temperature_bed <= 100) {
- i_add = (target_temperature_bed - 60) / 10;
- z_shift = eeprom_read_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i_add);
- z_shift_mm = z_shift / cs.axis_steps_per_unit[Z_AXIS];
- }else {
-
- z_shift_mm = temp_comp_interpolation(target_temperature_bed) / cs.axis_steps_per_unit[Z_AXIS];
- }
- printf_P(_N("\nZ shift applied:%.3f\n"), z_shift_mm);
- plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] - z_shift_mm, current_position[E_AXIS], homing_feedrate[Z_AXIS] / 40, active_extruder);
- st_synchronize();
- plan_set_z_position(current_position[Z_AXIS]);
- }
- else {
-
- }
- }
- #endif
- float temp_comp_interpolation(float inp_temperature) {
-
- int n, i, j;
- float h[10], a, b, c, d, sum, s[10] = { 0 }, x[10], F[10], f[10], m[10][10] = { 0 }, temp;
- int shift[10];
- int temp_C[10];
- n = 6;
- shift[0] = 0;
- for (i = 0; i < n; i++) {
- if (i > 0) {
-
- shift[i] = eeprom_read_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + (i - 1));
- }
- temp_C[i] = 50 + i * 10;
- #ifdef PINDA_THERMISTOR
- constexpr int start_compensating_temp = 35;
- temp_C[i] = start_compensating_temp + i * 5;
- #ifdef SUPERPINDA_SUPPORT
- static_assert(start_compensating_temp >= PINDA_MINTEMP, "Temperature compensation start point is lower than PINDA_MINTEMP.");
- #endif
- #else
- temp_C[i] = 50 + i * 10;
- #endif
- x[i] = (float)temp_C[i];
- f[i] = (float)shift[i];
- }
- if (inp_temperature < x[0]) return 0;
- for (i = n - 1; i>0; i--) {
- F[i] = (f[i] - f[i - 1]) / (x[i] - x[i - 1]);
- h[i - 1] = x[i] - x[i - 1];
- }
-
- for (i = 1; i<n - 1; i++) {
- m[i][i] = 2 * (h[i - 1] + h[i]);
- if (i != 1) {
- m[i][i - 1] = h[i - 1];
- m[i - 1][i] = h[i - 1];
- }
- m[i][n - 1] = 6 * (F[i + 1] - F[i]);
- }
-
- for (i = 1; i<n - 2; i++) {
- temp = (m[i + 1][i] / m[i][i]);
- for (j = 1; j <= n - 1; j++)
- m[i + 1][j] -= temp*m[i][j];
- }
-
- for (i = n - 2; i>0; i--) {
- sum = 0;
- for (j = i; j <= n - 2; j++)
- sum += m[i][j] * s[j];
- s[i] = (m[i][n - 1] - sum) / m[i][i];
- }
- for (i = 0; i<n - 1; i++)
- if ((x[i] <= inp_temperature && inp_temperature <= x[i + 1]) || (i == n-2 && inp_temperature > x[i + 1])) {
- a = (s[i + 1] - s[i]) / (6 * h[i]);
- b = s[i] / 2;
- c = (f[i + 1] - f[i]) / h[i] - (2 * h[i] * s[i] + s[i + 1] * h[i]) / 6;
- d = f[i];
- sum = a*pow((inp_temperature - x[i]), 3) + b*pow((inp_temperature - x[i]), 2) + c*(inp_temperature - x[i]) + d;
- }
- return sum;
- }
- #ifdef PINDA_THERMISTOR
- float temp_compensation_pinda_thermistor_offset(float temperature_pinda)
- {
- if (!eeprom_read_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE)) return 0;
- if (!calibration_status_pinda()) return 0;
- return temp_comp_interpolation(temperature_pinda) / cs.axis_steps_per_unit[Z_AXIS];
- }
- #endif
- void long_pause()
- {
- st_synchronize();
- start_pause_print = _millis();
-
- setAllTargetHotends(0);
-
- current_position[Z_AXIS] += Z_PAUSE_LIFT;
- clamp_to_software_endstops(current_position);
- plan_buffer_line_curposXYZE(15);
-
- current_position[X_AXIS] = X_PAUSE_POS;
- current_position[Y_AXIS] = Y_PAUSE_POS;
- plan_buffer_line_curposXYZE(50);
-
- fanSpeed = 0;
- }
- void serialecho_temperatures() {
- float tt = degHotend(active_extruder);
- SERIAL_PROTOCOLPGM("T:");
- SERIAL_PROTOCOL(tt);
- SERIAL_PROTOCOLPGM(" E:");
- SERIAL_PROTOCOL((int)active_extruder);
- SERIAL_PROTOCOLPGM(" B:");
- SERIAL_PROTOCOL_F(degBed(), 1);
- SERIAL_PROTOCOLLN();
- }
- #ifdef UVLO_SUPPORT
- void uvlo_drain_reset()
- {
-
- wdt_enable(WDTO_1S);
- WRITE(BEEPER,HIGH);
- lcd_clear();
- lcd_puts_at_P(0, 1, MSG_POWERPANIC_DETECTED);
- while(1);
- }
- void uvlo_()
- {
- unsigned long time_start = _millis();
- bool sd_print = card.sdprinting;
-
- #ifdef LCD_BL_PIN
- backlightMode = BACKLIGHT_MODE_DIM;
- backlightLevel_LOW = 0;
- backlight_update();
- #endif
- disable_x();
- disable_y();
-
- #ifdef TMC2130
- tmc2130_set_current_h(Z_AXIS, 20);
- tmc2130_set_current_r(Z_AXIS, 20);
- tmc2130_set_current_h(E_AXIS, 20);
- tmc2130_set_current_r(E_AXIS, 20);
- #endif
-
- uint8_t saved_target_temperature_bed = target_temperature_bed;
- uint16_t saved_target_temperature_ext = target_temperature[active_extruder];
- setAllTargetHotends(0);
- setTargetBed(0);
-
- long sd_position = sdpos_atomic;
- {
- uint16_t sdlen_planner = planner_calc_sd_length();
- sd_position -= sdlen_planner;
- uint16_t sdlen_cmdqueue = cmdqueue_calc_sd_length();
- sd_position -= sdlen_cmdqueue;
- if (sd_position < 0) sd_position = 0;
- }
-
- bool pos_invalid = XY_NO_RESTORE_FLAG;
- uint16_t feedrate_bckp;
- if (current_block && !pos_invalid)
- {
- memcpy(saved_target, current_block->gcode_target, sizeof(saved_target));
- feedrate_bckp = current_block->gcode_feedrate;
- }
- else
- {
- saved_target[0] = SAVED_TARGET_UNSET;
- feedrate_bckp = feedrate;
- }
-
-
-
- bool mbl_was_active = mbl.active;
- mbl.active = false;
-
-
-
- planner_abort_hard();
-
-
- float logical_z = current_position[Z_AXIS];
- if(mbl_was_active) logical_z -= mbl.get_z(st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS));
- eeprom_update_float((float*)EEPROM_UVLO_CURRENT_POSITION_Z, logical_z);
-
- eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E), current_position[E_AXIS]);
- eeprom_update_byte((uint8_t*)EEPROM_UVLO_E_ABS, (axis_relative_modes & E_AXIS_MASK)?0:1);
-
- cmdqueue_reset();
- card.sdprinting = false;
- saved_printing = true;
-
-
- sei();
-
- current_position[E_AXIS] -= default_retraction;
- plan_buffer_line_curposXYZE(95);
- st_synchronize();
- disable_e0();
-
-
-
-
- uint16_t z_res = tmc2130_get_res(Z_AXIS);
- uint16_t z_microsteps = tmc2130_rd_MSCNT(Z_AXIS);
- current_position[Z_AXIS] += float(1024 - z_microsteps)
- / (z_res * cs.axis_steps_per_unit[Z_AXIS])
- + UVLO_Z_AXIS_SHIFT;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS]/60);
- st_synchronize();
- poweroff_z();
-
- eeprom_update_dword((uint32_t*)(EEPROM_FILE_POSITION), sd_position);
-
- for (uint8_t mesh_point = 0; mesh_point < MESH_NUM_X_POINTS * MESH_NUM_Y_POINTS; ++ mesh_point) {
- uint8_t ix = mesh_point % MESH_NUM_X_POINTS;
- uint8_t iy = mesh_point / MESH_NUM_X_POINTS;
-
- int16_t v = mbl_was_active ? int16_t(floor(mbl.z_values[iy][ix] * 1000.f + 0.5f)) : 0;
- eeprom_update_word((uint16_t*)(EEPROM_UVLO_MESH_BED_LEVELING_FULL +2*mesh_point), *reinterpret_cast<uint16_t*>(&v));
- }
-
- eeprom_update_float((float*)EEPROM_UVLO_TINY_CURRENT_POSITION_Z, current_position[Z_AXIS]);
- z_microsteps = tmc2130_rd_MSCNT(Z_AXIS);
- eeprom_update_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS), z_microsteps);
-
- if (pos_invalid)
- eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0), X_COORD_INVALID);
- else
- {
- eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0), current_position[X_AXIS]);
- eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4), current_position[Y_AXIS]);
- }
-
- eeprom_update_word((uint16_t*)EEPROM_UVLO_FEEDRATE, feedrate_bckp);
- eeprom_update_word((uint16_t*)EEPROM_UVLO_FEEDMULTIPLY, feedmultiply);
- eeprom_update_word((uint16_t*)EEPROM_UVLO_TARGET_HOTEND, saved_target_temperature_ext);
- eeprom_update_byte((uint8_t*)EEPROM_UVLO_TARGET_BED, saved_target_temperature_bed);
- eeprom_update_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED, fanSpeed);
- eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0), extruder_multiplier[0]);
- #if EXTRUDERS > 1
- eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1), extruder_multiplier[1]);
- #if EXTRUDERS > 2
- eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2), extruder_multiplier[2]);
- #endif
- #endif
- eeprom_update_word((uint16_t*)(EEPROM_EXTRUDEMULTIPLY), (uint16_t)extrudemultiply);
- eeprom_update_float((float*)(EEPROM_UVLO_ACCELL), cs.acceleration);
- eeprom_update_float((float*)(EEPROM_UVLO_RETRACT_ACCELL), cs.retract_acceleration);
- eeprom_update_float((float*)(EEPROM_UVLO_TRAVEL_ACCELL), cs.travel_acceleration);
-
- eeprom_update_float((float*)(EEPROM_UVLO_SAVED_TARGET+0*4), saved_target[X_AXIS]);
- eeprom_update_float((float*)(EEPROM_UVLO_SAVED_TARGET+1*4), saved_target[Y_AXIS]);
- eeprom_update_float((float*)(EEPROM_UVLO_SAVED_TARGET+2*4), saved_target[Z_AXIS]);
- eeprom_update_float((float*)(EEPROM_UVLO_SAVED_TARGET+3*4), saved_target[E_AXIS]);
- #ifdef LIN_ADVANCE
- eeprom_update_float((float*)(EEPROM_UVLO_LA_K), extruder_advance_K);
- #endif
-
- if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, 1);
-
- eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1);
- eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1);
- printf_P(_N("UVLO - end %d\n"), _millis() - time_start);
- WRITE(BEEPER,HIGH);
-
- poweron_z();
- current_position[X_AXIS] = (current_position[X_AXIS] < 0.5f * (X_MIN_POS + X_MAX_POS)) ? X_MIN_POS : X_MAX_POS;
- plan_buffer_line_curposXYZE(500);
- st_synchronize();
- wdt_enable(WDTO_1S);
- while(1);
- }
- void uvlo_tiny()
- {
- unsigned long time_start = _millis();
-
- disable_x();
- disable_y();
- disable_e0();
- #ifdef TMC2130
- tmc2130_set_current_h(Z_AXIS, 20);
- tmc2130_set_current_r(Z_AXIS, 20);
- #endif
-
- setAllTargetHotends(0);
- setTargetBed(0);
-
-
-
-
- mbl.active = false;
- planner_abort_hard();
-
- if(fabs(current_position[Z_AXIS] - eeprom_read_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z))) >= 1.f/cs.axis_steps_per_unit[Z_AXIS])
- {
-
- cmdqueue_reset();
- card.sdprinting = false;
- saved_printing = true;
-
-
- sei();
-
- uint16_t z_res = tmc2130_get_res(Z_AXIS);
- uint16_t z_microsteps = tmc2130_rd_MSCNT(Z_AXIS);
- current_position[Z_AXIS] += float(1024 - z_microsteps)
- / (z_res * cs.axis_steps_per_unit[Z_AXIS])
- + UVLO_TINY_Z_AXIS_SHIFT;
- plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS]/60);
- st_synchronize();
- poweroff_z();
-
- eeprom_update_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z), current_position[Z_AXIS]);
-
- z_microsteps = tmc2130_rd_MSCNT(Z_AXIS);
- eeprom_update_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS), z_microsteps);
- }
-
- eeprom_update_byte((uint8_t*)EEPROM_UVLO,2);
-
- eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1);
- eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1);
- printf_P(_N("UVLO_TINY - end %d\n"), _millis() - time_start);
- uvlo_drain_reset();
- }
- #endif
- #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
- void setup_fan_interrupt() {
- DDRE &= ~(1 << 7);
- PORTE &= ~(1 << 7);
-
- EICRB &= ~(1 << 6);
- EICRB |= (1 << 7);
-
- EIMSK |= (1 << 7);
- }
- ISR(INT7_vect) {
-
- #ifdef FAN_SOFT_PWM
- if (!fan_measuring || (fanSpeedSoftPwm < MIN_PRINT_FAN_SPEED)) return;
- #else
- if (fanSpeed < MIN_PRINT_FAN_SPEED) return;
- #endif
- if ((1 << 6) & EICRB) {
- t_fan_rising_edge = millis_nc();
- }
- else {
- if ((millis_nc() - t_fan_rising_edge) >= FAN_PULSE_WIDTH_LIMIT) {
- fan_edge_counter[1] += 2;
- }
- }
- EICRB ^= (1 << 6);
- }
- #endif
- #ifdef UVLO_SUPPORT
- void setup_uvlo_interrupt() {
- DDRE &= ~(1 << 4);
- PORTE &= ~(1 << 4);
-
- EICRB |= (1 << 0);
- EICRB &= ~(1 << 1);
-
- EIMSK |= (1 << 4);
-
- if(!(PINE & (1 << 4)) && eeprom_read_byte((uint8_t*)EEPROM_UVLO))
- {
- SERIAL_ECHOLNPGM("INT4");
- uvlo_drain_reset();
- }
- }
- ISR(INT4_vect) {
- EIMSK &= ~(1 << 4);
- SERIAL_ECHOLNPGM("INT4");
-
- if(PRINTER_ACTIVE && (!(eeprom_read_byte((uint8_t*)EEPROM_UVLO)))) uvlo_();
- if(eeprom_read_byte((uint8_t*)EEPROM_UVLO)) uvlo_tiny();
- }
- void recover_print(uint8_t automatic) {
- char cmd[30];
- lcd_update_enable(true);
- lcd_update(2);
- lcd_setstatuspgm(_i("Recovering print"));
-
- bool mbl_was_active = recover_machine_state_after_power_panic();
-
-
- if(eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 1)
- {
- sprintf_P(cmd, PSTR("G1 Z%.3f F800"), current_position[Z_AXIS] + 25);
- enquecommand(cmd);
- }
-
-
- enquecommand_P(PSTR("G28 X Y"));
-
- sprintf_P(cmd, PSTR("M104 S%d"), target_temperature[active_extruder]);
- enquecommand(cmd);
- sprintf_P(cmd, PSTR("M190 S%d"), target_temperature_bed);
- enquecommand(cmd);
- sprintf_P(cmd, PSTR("M109 S%d"), target_temperature[active_extruder]);
- enquecommand(cmd);
- enquecommand_P(PSTR("M83"));
-
- if(automatic == 0){
-
- enquecommand_P(PSTR("G1 E5 F120"));
-
- sprintf_P(cmd, PSTR("G1 E%-0.3f F2700"), default_retraction);
- enquecommand(cmd);
- }
- printf_P(_N("After waiting for temp:\nCurrent pos X_AXIS:%.3f\nCurrent pos Y_AXIS:%.3f\n"), current_position[X_AXIS], current_position[Y_AXIS]);
-
- restore_print_from_eeprom(mbl_was_active);
- printf_P(_N("Current pos Z_AXIS:%.3f\nCurrent pos E_AXIS:%.3f\n"), current_position[Z_AXIS], current_position[E_AXIS]);
- }
- bool recover_machine_state_after_power_panic()
- {
-
- current_position[X_AXIS] = 0;
- current_position[Y_AXIS] = 0;
-
-
- bool mbl_was_active = false;
- for (int8_t mesh_point = 0; mesh_point < MESH_NUM_X_POINTS * MESH_NUM_Y_POINTS; ++ mesh_point) {
- uint8_t ix = mesh_point % MESH_NUM_X_POINTS;
- uint8_t iy = mesh_point / MESH_NUM_X_POINTS;
-
- int16_t v;
- eeprom_read_block(&v, (void*)(EEPROM_UVLO_MESH_BED_LEVELING_FULL+2*mesh_point), 2);
- if (v != 0)
- mbl_was_active = true;
- mbl.z_values[iy][ix] = float(v) * 0.001f;
- }
-
-
- current_position[Z_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z));
-
- current_position[E_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E));
-
- world2machine_initialize();
-
-
- babystep_load();
-
-
- clamp_to_software_endstops(current_position);
- set_destination_to_current();
- plan_set_position_curposXYZE();
- SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial ");
- print_world_coordinates();
-
- axis_known_position[Z_AXIS] = true;
- enable_z();
-
- target_temperature[active_extruder] = eeprom_read_word((uint16_t*)EEPROM_UVLO_TARGET_HOTEND);
- target_temperature_bed = eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED);
-
- extruder_multiplier[0] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0));
- #if EXTRUDERS > 1
- extruder_multiplier[1] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1));
- #if EXTRUDERS > 2
- extruder_multiplier[2] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2));
- #endif
- #endif
- extrudemultiply = (int)eeprom_read_word((uint16_t*)(EEPROM_EXTRUDEMULTIPLY));
-
- saved_target[X_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_SAVED_TARGET+0*4));
- saved_target[Y_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_SAVED_TARGET+1*4));
- saved_target[Z_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_SAVED_TARGET+2*4));
- saved_target[E_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_SAVED_TARGET+3*4));
- #ifdef LIN_ADVANCE
- extruder_advance_K = eeprom_read_float((float*)EEPROM_UVLO_LA_K);
- #endif
- return mbl_was_active;
- }
- void restore_print_from_eeprom(bool mbl_was_active) {
- int feedrate_rec;
- int feedmultiply_rec;
- uint8_t fan_speed_rec;
- char cmd[48];
- char filename[13];
- uint8_t depth = 0;
- char dir_name[9];
- fan_speed_rec = eeprom_read_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED);
- feedrate_rec = eeprom_read_word((uint16_t*)EEPROM_UVLO_FEEDRATE);
- feedmultiply_rec = eeprom_read_word((uint16_t*)EEPROM_UVLO_FEEDMULTIPLY);
- SERIAL_ECHOPGM("Feedrate:");
- MYSERIAL.print(feedrate_rec);
- SERIAL_ECHOPGM(", feedmultiply:");
- MYSERIAL.println(feedmultiply_rec);
- depth = eeprom_read_byte((uint8_t*)EEPROM_DIR_DEPTH);
-
- MYSERIAL.println(int(depth));
- for (uint8_t i = 0; i < depth; i++) {
- for (uint8_t j = 0; j < 8; j++) {
- dir_name[j] = eeprom_read_byte((uint8_t*)EEPROM_DIRS + j + 8 * i);
- }
- dir_name[8] = '\0';
- MYSERIAL.println(dir_name);
-
- card.chdir(dir_name, false);
- }
- for (uint8_t i = 0; i < 8; i++) {
- filename[i] = eeprom_read_byte((uint8_t*)EEPROM_FILENAME + i);
- }
- filename[8] = '\0';
- MYSERIAL.print(filename);
- strcat_P(filename, PSTR(".gco"));
- sprintf_P(cmd, PSTR("M23 %s"), filename);
- enquecommand(cmd);
- uint32_t position = eeprom_read_dword((uint32_t*)(EEPROM_FILE_POSITION));
- SERIAL_ECHOPGM("Position read from eeprom:");
- MYSERIAL.println(position);
-
-
- float pos_x = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0));
- float pos_y = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4));
- if (pos_x != X_COORD_INVALID)
- {
- sprintf_P(cmd, PSTR("G1 X%f Y%f F3000"), pos_x, pos_y);
- enquecommand(cmd);
- }
-
- if (mbl_was_active)
- enquecommand_P(PSTR("PRUSA MBL V1"));
-
- sprintf_P(cmd, PSTR("G1 Z%f"), eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z)));
- enquecommand(cmd);
-
- float acceleration = eeprom_read_float((float*)(EEPROM_UVLO_ACCELL));
- float retract_acceleration = eeprom_read_float((float*)(EEPROM_UVLO_RETRACT_ACCELL));
- float travel_acceleration = eeprom_read_float((float*)(EEPROM_UVLO_TRAVEL_ACCELL));
- sprintf_P(cmd, PSTR("M204 P%f R%f T%f"), acceleration, retract_acceleration, travel_acceleration);
- enquecommand(cmd);
-
- sprintf_P(cmd, PSTR("G1 E%0.3f F2700"), default_retraction);
- enquecommand(cmd);
-
- float pos_e = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E));
- sprintf_P(cmd, PSTR("G92 E%6.3f"), pos_e);
- enquecommand(cmd);
- if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS))
- enquecommand_P(PSTR("M82"));
-
- sprintf_P(cmd, PSTR("G1 F%d"), feedrate_rec);
- enquecommand(cmd);
- sprintf_P(cmd, PSTR("M220 S%d"), feedmultiply_rec);
- enquecommand(cmd);
-
- strcpy_P(cmd, PSTR("M106 S"));
- strcat(cmd, itostr3(int(fan_speed_rec)));
- enquecommand(cmd);
-
- sprintf_P(cmd, PSTR("M26 S%lu"), position);
- enquecommand(cmd);
- enquecommand_P(PSTR("G4 S0"));
- enquecommand_P(PSTR("PRUSA uvlo"));
- }
- #endif
- void stop_and_save_print_to_ram(float z_move, float e_move)
- {
- if (saved_printing) return;
- #if 0
- unsigned char nplanner_blocks;
- #endif
- unsigned char nlines;
- uint16_t sdlen_planner;
- uint16_t sdlen_cmdqueue;
-
- cli();
- if (card.sdprinting) {
- #if 0
- nplanner_blocks = number_of_blocks();
- #endif
- saved_sdpos = sdpos_atomic;
- sdlen_planner = planner_calc_sd_length();
- saved_sdpos -= sdlen_planner;
- sdlen_cmdqueue = cmdqueue_calc_sd_length();
- saved_sdpos -= sdlen_cmdqueue;
- saved_printing_type = PRINTING_TYPE_SD;
- }
- else if (usb_timer.running()) {
- saved_sdpos = gcode_LastN;
-
- nlines = planner_calc_sd_length();
- saved_sdpos -= nlines;
- saved_sdpos -= buflen;
- saved_printing_type = PRINTING_TYPE_USB;
- }
- else {
- saved_printing_type = PRINTING_TYPE_NONE;
-
- }
- #if 0
- SERIAL_ECHOPGM("SDPOS_ATOMIC="); MYSERIAL.println(sdpos_atomic, DEC);
- SERIAL_ECHOPGM("SDPOS="); MYSERIAL.println(card.get_sdpos(), DEC);
- SERIAL_ECHOPGM("SDLEN_PLAN="); MYSERIAL.println(sdlen_planner, DEC);
- SERIAL_ECHOPGM("SDLEN_CMDQ="); MYSERIAL.println(sdlen_cmdqueue, DEC);
- SERIAL_ECHOPGM("PLANNERBLOCKS="); MYSERIAL.println(int(nplanner_blocks), DEC);
- SERIAL_ECHOPGM("SDSAVED="); MYSERIAL.println(saved_sdpos, DEC);
-
- {
- card.setIndex(saved_sdpos);
- SERIAL_ECHOLNPGM("Content of planner buffer: ");
- for (unsigned int idx = 0; idx < sdlen_planner; ++ idx)
- MYSERIAL.print(char(card.get()));
- SERIAL_ECHOLNPGM("Content of command buffer: ");
- for (unsigned int idx = 0; idx < sdlen_cmdqueue; ++ idx)
- MYSERIAL.print(char(card.get()));
- SERIAL_ECHOLNPGM("End of command buffer");
- }
- {
-
- card.setIndex(saved_sdpos);
- int8_t iline = 0;
- for (unsigned char idx = block_buffer_tail; idx != block_buffer_head; idx = (idx + 1) & (BLOCK_BUFFER_SIZE - 1), ++ iline) {
- SERIAL_ECHOPGM("Planner line (from file): ");
- MYSERIAL.print(int(iline), DEC);
- SERIAL_ECHOPGM(", length: ");
- MYSERIAL.print(block_buffer[idx].sdlen, DEC);
- SERIAL_ECHOPGM(", steps: (");
- MYSERIAL.print(block_buffer[idx].steps_x, DEC);
- SERIAL_ECHOPGM(",");
- MYSERIAL.print(block_buffer[idx].steps_y, DEC);
- SERIAL_ECHOPGM(",");
- MYSERIAL.print(block_buffer[idx].steps_z, DEC);
- SERIAL_ECHOPGM(",");
- MYSERIAL.print(block_buffer[idx].steps_e, DEC);
- SERIAL_ECHOPGM("), events: ");
- MYSERIAL.println(block_buffer[idx].step_event_count, DEC);
- for (int len = block_buffer[idx].sdlen; len > 0; -- len)
- MYSERIAL.print(char(card.get()));
- }
- }
- {
-
- int8_t iline = 0;
- union {
- struct {
- char lo;
- char hi;
- } lohi;
- uint16_t value;
- } sdlen_single;
- int _bufindr = bufindr;
- for (int _buflen = buflen; _buflen > 0; ++ iline) {
- if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
- sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
- sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
- }
- SERIAL_ECHOPGM("Buffer line (from buffer): ");
- MYSERIAL.print(int(iline), DEC);
- SERIAL_ECHOPGM(", type: ");
- MYSERIAL.print(int(cmdbuffer[_bufindr]), DEC);
- SERIAL_ECHOPGM(", len: ");
- MYSERIAL.println(sdlen_single.value, DEC);
-
- MYSERIAL.println(cmdbuffer + _bufindr + CMDHDRSIZE);
- SERIAL_ECHOPGM("Buffer line (from file): ");
- MYSERIAL.println(int(iline), DEC);
- for (; sdlen_single.value > 0; -- sdlen_single.value)
- MYSERIAL.print(char(card.get()));
- if (-- _buflen == 0)
- break;
-
- for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
-
- for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
-
- if (_bufindr == sizeof(cmdbuffer)) {
-
- for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
- }
- }
- }
- #endif
-
- bool pos_invalid = XY_NO_RESTORE_FLAG;
- if (current_block && !pos_invalid)
- {
- memcpy(saved_target, current_block->gcode_target, sizeof(saved_target));
- saved_feedrate2 = current_block->gcode_feedrate;
- }
- else
- {
- saved_target[0] = SAVED_TARGET_UNSET;
- saved_feedrate2 = feedrate;
- }
- planner_abort_hard();
- memcpy(saved_pos, current_position, sizeof(saved_pos));
- if (pos_invalid) saved_pos[X_AXIS] = X_COORD_INVALID;
- saved_feedmultiply2 = feedmultiply;
- saved_active_extruder = active_extruder;
- saved_extruder_temperature = degTargetHotend(active_extruder);
- saved_extruder_relative_mode = axis_relative_modes & E_AXIS_MASK;
- saved_fanSpeed = fanSpeed;
- cmdqueue_reset();
- card.sdprinting = false;
- saved_printing = true;
-
- st_reset_timer();
- sei();
- if ((z_move != 0) || (e_move != 0)) {
- #if 1
-
-
-
- char buf[48];
- if(e_move)
- {
-
- if(!saved_extruder_relative_mode){
- enquecommand(PSTR("M83"), true);
- }
-
-
-
-
-
-
- sprintf_P(buf, PSTR("G1 E%-0.3f F2700"), e_move);
- enquecommand(buf, false);
- }
- if(z_move)
- {
-
- sprintf_P(buf, PSTR("G1 Z%-0.3f F%-0.3f"), saved_pos[Z_AXIS] + z_move, homing_feedrate[Z_AXIS]);
- enquecommand(buf, false);
- }
-
-
- repeatcommand_front();
- #else
- plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS] + z_move, saved_pos[E_AXIS] + e_move, homing_feedrate[Z_AXIS], active_extruder);
- st_synchronize();
- memcpy(current_position, saved_pos, sizeof(saved_pos));
- set_destination_to_current();
- #endif
- waiting_inside_plan_buffer_line_print_aborted = true;
- }
- }
- void restore_print_from_ram_and_continue(float e_move)
- {
- if (!saved_printing) return;
-
- #ifdef FANCHECK
-
- if ((fan_check_error != EFCE_OK) && (fan_check_error != EFCE_FIXED)) return;
- if (fan_check_error == EFCE_FIXED) fan_check_error = EFCE_OK;
- #endif
-
- active_extruder = saved_active_extruder;
- fanSpeed = saved_fanSpeed;
- if (degTargetHotend(saved_active_extruder) != saved_extruder_temperature)
- {
- setTargetHotendSafe(saved_extruder_temperature, saved_active_extruder);
- heating_status = HeatingStatus::EXTRUDER_HEATING;
- wait_for_heater(_millis(), saved_active_extruder);
- heating_status = HeatingStatus::EXTRUDER_HEATING_COMPLETE;
- }
- axis_relative_modes ^= (-saved_extruder_relative_mode ^ axis_relative_modes) & E_AXIS_MASK;
- float e = saved_pos[E_AXIS] - e_move;
- plan_set_e_position(e);
-
- #ifdef FANCHECK
- fans_check_enabled = false;
- #endif
-
- if (saved_pos[X_AXIS] == X_COORD_INVALID)
- {
- saved_pos[X_AXIS] = current_position[X_AXIS];
- saved_pos[Y_AXIS] = current_position[Y_AXIS];
- }
-
- plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], current_position[Z_AXIS], saved_pos[E_AXIS] - e_move, homing_feedrate[Z_AXIS]/13, active_extruder);
-
- plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], saved_pos[E_AXIS] - e_move, homing_feedrate[Z_AXIS]/13, active_extruder);
-
- plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], saved_pos[E_AXIS], FILAMENTCHANGE_RFEED, active_extruder);
- st_synchronize();
- #ifdef FANCHECK
- fans_check_enabled = true;
- #endif
-
- feedrate = saved_feedrate2;
- feedmultiply = saved_feedmultiply2;
- memcpy(current_position, saved_pos, sizeof(saved_pos));
- set_destination_to_current();
- if (saved_printing_type == PRINTING_TYPE_SD) {
- card.setIndex(saved_sdpos);
- sdpos_atomic = saved_sdpos;
- card.sdprinting = true;
- }
- else if (saved_printing_type == PRINTING_TYPE_USB) {
- gcode_LastN = saved_sdpos;
- serial_count = 0;
- FlushSerialRequestResend();
- }
- else {
-
- }
- lcd_setstatuspgm(MSG_WELCOME);
- saved_printing_type = PRINTING_TYPE_NONE;
- saved_printing = false;
- waiting_inside_plan_buffer_line_print_aborted = true;
- }
- void cancel_saved_printing()
- {
- eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0);
- saved_target[0] = SAVED_TARGET_UNSET;
- saved_printing_type = PRINTING_TYPE_NONE;
- saved_printing = false;
- }
- void print_world_coordinates()
- {
- printf_P(_N("world coordinates: (%.3f, %.3f, %.3f)\n"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]);
- }
- void print_physical_coordinates()
- {
- printf_P(_N("physical coordinates: (%.3f, %.3f, %.3f)\n"), st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS), st_get_position_mm(Z_AXIS));
- }
- void print_mesh_bed_leveling_table()
- {
- SERIAL_ECHOPGM("mesh bed leveling: ");
- for (int8_t y = 0; y < MESH_NUM_Y_POINTS; ++ y)
- for (int8_t x = 0; x < MESH_NUM_Y_POINTS; ++ x) {
- MYSERIAL.print(mbl.z_values[y][x], 3);
- SERIAL_ECHO(' ');
- }
- SERIAL_ECHOLN();
- }
- uint8_t calc_percent_done()
- {
-
- uint8_t percent_done = 0;
- #ifdef TMC2130
- if (SilentModeMenu == SILENT_MODE_OFF && print_percent_done_normal <= 100)
- {
- percent_done = print_percent_done_normal;
- }
- else if (print_percent_done_silent <= 100)
- {
- percent_done = print_percent_done_silent;
- }
- #else
- if (print_percent_done_normal <= 100)
- {
- percent_done = print_percent_done_normal;
- }
- #endif
- else
- {
- percent_done = card.percentDone();
- }
- return percent_done;
- }
- static void print_time_remaining_init()
- {
- print_time_remaining_normal = PRINT_TIME_REMAINING_INIT;
- print_percent_done_normal = PRINT_PERCENT_DONE_INIT;
- print_time_remaining_silent = PRINT_TIME_REMAINING_INIT;
- print_percent_done_silent = PRINT_PERCENT_DONE_INIT;
- print_time_to_change_normal = PRINT_TIME_REMAINING_INIT;
- print_time_to_change_silent = PRINT_TIME_REMAINING_INIT;
- }
- void load_filament_final_feed()
- {
- current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FINAL);
- }
- void load_filament_final_retract()
- {
- current_position[E_AXIS] -= FILAMENTCHANGE_LOADRETRACT;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FIRST);
- }
- void M600_check_state(float nozzle_temp)
- {
- lcd_change_fil_state = 0;
- while (lcd_change_fil_state != 1)
- {
- lcd_change_fil_state = 0;
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- lcd_alright();
- KEEPALIVE_STATE(IN_HANDLER);
- switch(lcd_change_fil_state)
- {
-
- case 2:
- if (mmu_enabled)
- mmu_M600_load_filament(false, nozzle_temp);
- else
- M600_load_filament_movements();
- break;
-
- case 3:
- st_synchronize();
- load_filament_final_feed();
- lcd_loading_color();
- st_synchronize();
- break;
-
- default:
- lcd_change_success();
- break;
- }
- }
- }
- void M600_wait_for_user(float HotendTempBckp) {
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- int counterBeep = 0;
- unsigned long waiting_start_time = _millis();
- uint8_t wait_for_user_state = 0;
- lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD));
- bool bFirst=true;
- while (!(wait_for_user_state == 0 && lcd_clicked())){
- manage_heater();
- manage_inactivity(true);
- #if BEEPER > 0
- if (counterBeep == 500) {
- counterBeep = 0;
- }
- SET_OUTPUT(BEEPER);
- if (counterBeep == 0) {
- if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
- {
- bFirst=false;
- WRITE(BEEPER, HIGH);
- }
- }
- if (counterBeep == 20) {
- WRITE(BEEPER, LOW);
- }
-
- counterBeep++;
- #endif
-
- switch (wait_for_user_state) {
- case 0:
- delay_keep_alive(4);
- if (_millis() > waiting_start_time + (unsigned long)M600_TIMEOUT * 1000) {
- lcd_display_message_fullscreen_P(_i("Press the knob to preheat nozzle and continue."));
- wait_for_user_state = 1;
- setAllTargetHotends(0);
- st_synchronize();
- disable_e0();
- disable_e1();
- disable_e2();
- }
- break;
- case 1:
- delay_keep_alive(4);
-
- if (lcd_clicked()) {
- setTargetHotend(HotendTempBckp, active_extruder);
- lcd_wait_for_heater();
- wait_for_user_state = 2;
- }
- break;
- case 2:
- if (fabs(degTargetHotend(active_extruder) - degHotend(active_extruder)) < 1) {
- lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD));
- waiting_start_time = _millis();
- wait_for_user_state = 0;
- }
- else {
- counterBeep = 20;
- lcd_set_cursor(1, 4);
- lcd_printf_P(PSTR("%3d"), (int16_t)degHotend(active_extruder));
- }
- break;
- }
- }
- WRITE(BEEPER, LOW);
- }
- void M600_load_filament_movements()
- {
- current_position[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED;
- plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FIRST);
- load_filament_final_feed();
- lcd_loading_filament();
- st_synchronize();
- }
- void M600_load_filament() {
-
- lcd_wait_interact();
-
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- #ifdef PAT9125
- fsensor_autoload_check_start();
- #endif
- while(!lcd_clicked())
- {
- manage_heater();
- manage_inactivity(true);
- #ifdef FILAMENT_SENSOR
- if (fsensor_check_autoload())
- {
- Sound_MakeCustom(50,1000,false);
- break;
- }
- #endif
- }
- #ifdef PAT9125
- fsensor_autoload_check_stop();
- #endif
- KEEPALIVE_STATE(IN_HANDLER);
- #ifdef FSENSOR_QUALITY
- fsensor_oq_meassure_start(70);
- #endif
- M600_load_filament_movements();
- Sound_MakeCustom(50,1000,false);
- #ifdef FSENSOR_QUALITY
- fsensor_oq_meassure_stop();
- if (!fsensor_oq_result())
- {
- bool disable = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Fil. sensor response is poor, disable it?"), false, true);
- lcd_update_enable(true);
- lcd_update(2);
- if (disable)
- fsensor_disable();
- }
- #endif
- lcd_update_enable(false);
- }
- void marlin_wait_for_click()
- {
- int8_t busy_state_backup = busy_state;
- KEEPALIVE_STATE(PAUSED_FOR_USER);
- lcd_consume_click();
- while(!lcd_clicked())
- {
- manage_heater();
- manage_inactivity(true);
- lcd_update(0);
- }
- KEEPALIVE_STATE(busy_state_backup);
- }
- #define FIL_LOAD_LENGTH 60
- #ifdef PSU_Delta
- bool bEnableForce_z;
- void init_force_z()
- {
- WRITE(Z_ENABLE_PIN,Z_ENABLE_ON);
- bEnableForce_z=true;
- disable_force_z();
- }
- void check_force_z()
- {
- if(!(bEnableForce_z||eeprom_read_byte((uint8_t*)EEPROM_SILENT)))
- init_force_z();
- }
- void disable_force_z()
- {
- if(!bEnableForce_z) return;
- bEnableForce_z=false;
-
- #ifdef TMC2130
- tmc2130_mode=TMC2130_MODE_SILENT;
- update_mode_profile();
- tmc2130_init(TMCInitParams(true, FarmOrUserECool()));
- #endif
- }
- void enable_force_z()
- {
- if(bEnableForce_z)
- return;
- bEnableForce_z=true;
- #ifdef TMC2130
- tmc2130_mode=eeprom_read_byte((uint8_t*)EEPROM_SILENT)?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL;
- update_mode_profile();
- tmc2130_init(TMCInitParams(true, FarmOrUserECool()));
- #endif
- WRITE(Z_ENABLE_PIN,Z_ENABLE_ON);
- }
- #endif
|