12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949 |
- /* additional text */
- Blockly.Blocks['text_split_string_by_delimiter'] = {
- init: function () {
- this.appendValueInput("string_input")
- .setCheck(null)
- .appendField(Blockly.Msg.text_additional_delimiter_string);
- this.appendDummyInput()
- .appendField(Blockly.Msg.text_additional_delimiter_deli)
- .appendField(new Blockly.FieldTextInput(","), "delimiter")
- .appendField(Blockly.Msg.text_additional_delimiter_end);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#33cc99");
- this.setTooltip(Blockly.Msg.Text_Split_String_By_Delimiter_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['text_split_string_by_delimiter'] = function (block) {
- var value_string_input = Blockly.Python.valueToCode(block, 'string_input', Blockly.Python.ORDER_ATOMIC);
- var text_delimiter = block.getFieldValue('delimiter');
- // TODO: Assemble Python into code variable.
- var code = value_string_input + '.split("' + text_delimiter + '")';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* additional math */
- Blockly.Blocks['math_degrad'] = {
- init: function () {
- this.appendValueInput("convert")
- .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.math_degrad_rad, "radians"],
- [Blockly.Msg.math_degrad_deg, "degrees"]
- ]), "rad_deg");
- this.setOutput(true, null);
- this.setColour("#5472ea");
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('rad_deg');
- var TOOLTIPS = {
- 'radians': Blockly.Msg.Math_Degrad_RAD_TOOLTIP,
- 'degrees': Blockly.Msg.Math_Degrad_DEG_TOOLTIP
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['math_degrad'] = function (block) {
- var dropdown_rad_deg = block.getFieldValue('rad_deg');
- var value_convert = Blockly.Python.valueToCode(block, 'convert', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_math'] = 'import math\n';
- var code = 'math.' + dropdown_rad_deg + '(' + value_convert + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* Serial Comm. */
- Blockly.Blocks['serial_comm_print'] = {
- init: function () {
- this.appendValueInput("serial_comm_input")
- .setCheck("String")
- .appendField(Blockly.Msg.serialcomm_print);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#22b845");
- this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['serial_comm_print'] = function (block) {
- var value_name = Blockly.Python.valueToCode(block, 'serial_comm_input', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'print(' + value_name + ')\n';
- return code;
- };
- Blockly.Blocks['serial_send_data_to_control_panel'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/control.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField("9600")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.appendValueInput("NAME")
- // .appendField(Blockly.Msg.serial_send_data_on)
- .appendField(Blockly.Msg.serial_send_data_on_send_data_control_panel);
- this.setInputsInline(false);
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.serial_send_data_on_send_data_control_panel);
- }
- };
- Blockly.Python['serial_send_data_to_control_panel'] = function (block) {
- var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_["831_import_serial"] = `import serial
- SERIAL = serial.Serial("/dev/ttyS1",9600)`
- var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
- SERIAL.write(uart_data)
- `;
- return code;
- };
- /*
- INPUT & OUTPUT
- */
- var ESP32_IO_COLOR = "#ff8027";
- Blockly.Blocks['esp32_main_controller_text_print'] = {
- init: function () {
- this.jsonInit({
- "message0": Blockly.Msg.TEXT_PRINT_TITLE,
- "args0": [{
- "type": "input_value",
- "name": "TEXT"
- }],
- "previousStatement": null,
- "nextStatement": null,
- "colour": "#33cc99",
- "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
- "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
- });
- }
- };
- Blockly.Python['esp32_main_controller_text_print'] = function (block) {
- // Print statement.
- var msg = Blockly.Python.valueToCode(block, 'TEXT',
- Blockly.Python.ORDER_NONE) || '___';
- return 'print(' + msg + ')\n';
- };
- Blockly.Blocks['esp32_main_controller_text'] = {
- init: function () {
- this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
- this.setColour("#33cc99");
- this.appendDummyInput()
- .appendField(this.newQuote_(true))
- .appendField(new Blockly.FieldTextInput(''), 'TEXT')
- .appendField(this.newQuote_(false));
- this.setOutput(true, 'String');
- // Assign 'this' to a variable for use in the tooltip closure below.
- var thisBlock = this;
- // Text block is trivial. Use tooltip of parent block if it exists.
- this.setTooltip(function () {
- var parent = thisBlock.getParent();
- return (parent && parent.getInputsInline() && parent.tooltip) ||
- Blockly.Msg.TEXT_TEXT_TOOLTIP;
- });
- },
- newQuote_: function (open) {
- if (open == this.RTL) {
- var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
- } else {
- var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
- }
- return new Blockly.FieldImage(file, 12, 12, '"');
- }
- };
- Blockly.Python['esp32_main_controller_text'] = function (block) {
- // Text value.
- var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_get)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"],
- ["PH7", "7"],
- ["PH8", "8"],
- ["PH13", "13"],
- ["CLK", "PC0"],
- ["CS0", "PC1"],
- ["MOSI", "PC2"],
- ["MISO", "PC3"],
- ["TXD", "PG6"],
- ["RXD", "PG7"],
- ["P0", "21"],
- ["P1", "22"],
- ["P2", "23"],
- ["P3", "24"],
- ["A0", "25"],
- ["A1", "26"],
- ["A2", "27"],
- ["A3", "28"]
- ]), "analog_pin");
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- this.setTooltip("");
- }
- };
- Blockly.Python['esp32_main_controller_io_from_digital_pin'] = function (block) {
- var value_name = block.getFieldValue('analog_pin');
- // TODO: Assemble Python into code variable.
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_os'] = `import os`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ""
- if (value_name == 2 || value_name == 1) {
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable('_Math_gpio_' + value_name, `_Math_gpio_${value_name} = multiFuncGpio(${value_name - 1},2)`, true);
- code = `_Math_gpio_${value_name}.digitalRead()`;
- } else if (value_name == "PG6" || value_name == "PG7" || value_name == "PC0" || value_name == "PC1" || value_name == "PC2" || value_name == "PC3") {
- // Blockly.Python
- Blockly.Python.addFunction("pinMode", `def pinMode(pin,mode):
- os.system(f"echo {pin} {mode} > /sys/kernel/debug/sunxi_pinctrl/function")
- `)
- Blockly.Python.addFunction("digitalWrite", `def digitalWrite(pin,level):
- if pin == "PC1" or pin == "PC0" or pin == "PC2" or pin == "PC3":
- os.system(f"echo {pin} 1 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system(f"echo {pin} {level} > /sys/kernel/debug/sunxi_pinctrl/data")
- `)
- Blockly.Python.addFunction("digitalRead", `def digitalRead(pin):
- if pin == "PC1" or pin == "PC0" or pin == "PC2" or pin == "PC3":
- os.system("echo {pin} 2 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system(f"echo {pin} > /sys/kernel/debug/sunxi_pinctrl/sunxi_pin")
- return int(os.popen("cat /sys/kernel/debug/sunxi_pinctrl/data").read()[-2:])
- `)
- Blockly.Python.addFunction("setToSpi", `def setToSpi():
- os.system("echo PC0 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC1 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC2 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC3 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC0 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC1 1 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC2 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC3 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- `)
- Blockly.Python.addFunction("setToSerial", `def setToSerial():
- os.system("echo PG6 5 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PG7 5 > /sys/kernel/debug/sunxi_pinctrl/function")
- `)
- Blockly.Python.addVariable(`pinMode${value_name}`, `pinMode("${value_name}","0")`, true)
- code = `digitalRead("${value_name}")`
- }
- else if (value_name == 7 || value_name == 8 || value_name == 13) {
- Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON `;
- Blockly.Python.addFunction("getBUTTONNum", `def getBUTTONNum(data):
- if data:
- return 0
- else:
- return 1
- `)
- Blockly.Python.addVariable('_Math_gpio_' + value_name, `_Math_gpio_${value_name} = BUTTON(${value_name})`, true);
- code = `getBUTTONNum(_Math_gpio_${value_name}.is_pressed())`;
- } else {
- let gpioNum = value_name - 21
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `InputGpio = extGpio()`;
- Blockly.Python.addVariable('InputGpio' + value_name, `InputGpio.pinMode(${gpioNum},0)`, true);
- code = `InputGpio.digitalRead(${gpioNum})`
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_io_high_low'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.io_high, "1"],
- [Blockly.Msg.io_low, "0"]
- ]), "high_low");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('high_low');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_high),
- '0': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_low)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_high_low'] = function (block) {
- var dropdown_name = block.getFieldValue('high_low');
- // TODO: Assemble Python into code variable.
- var code = dropdown_name;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_io_analog_pin_1'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Esp32_Main_Controller_Io_analog_pin_1)
- .appendField(new Blockly.FieldDropdown([
- ["0", "0"],
- ["1", "1"],
- ["2", "2"],
- ["3", "3"],
- ["13", "13"],
- ["14", "14"],
- ["15", "15"],
- ["17", "17"],
- ["21", "21"],
- ["22", "22"],
- ["23", "23"],
- ["24", "24"],
- ["29", "29"],
- ["30", "30"]
- ]), "analog_pin");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('analog_pin');
- var TOOLTIPS = {
- '0': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '0'),
- '1': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '1'),
- '2': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '2'),
- '3': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '3'),
- '13': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '13'),
- '14': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '14'),
- '15': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '15'),
- '17': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '17'),
- '21': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '21'),
- '22': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '22'),
- '23': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '23'),
- '24': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '24'),
- '29': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '29'),
- '30': Blockly.Msg.esp32_main_controller_io_analog_pin_1_TOOLTIP.replace('%1', '30')
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_analog_pin_1'] = function (block) {
- var dropdown_name = block.getFieldValue('analog_pin');
- // TODO: Assemble Python into code variable.
- var code = dropdown_name;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_io_analog_pin_2'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Esp32_Main_Controller_Io_analog_pin_2)
- .appendField(new Blockly.FieldDropdown([
- ["4", "4"],
- ["12", "12"],
- ["13", "13"],
- ["14", "14"],
- ["15", "15"],
- ["16", "16"],
- ["17", "17"],
- ["21", "21"],
- ["22", "22"],
- ["25", "25"],
- ["27", "27"],
- ["34", "34"],
- ["35", "35"],
- ["36", "36"]
- ]), "analog_pin");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('analog_pin');
- var TOOLTIPS = {
- '4': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '4'),
- '12': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '12'),
- '13': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '13'),
- '14': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '14'),
- '15': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '15'),
- '16': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '16'),
- '17': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '17'),
- '21': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '21'),
- '22': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '22'),
- '25': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '25'),
- '27': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '27'),
- '34': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '34'),
- '35': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '35'),
- '36': Blockly.Msg.esp32_main_controller_io_analog_pin_2_TOOLTIP.replace('%1', '36')
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_analog_pin_2'] = function (block) {
- var dropdown_name = block.getFieldValue('analog_pin');
- // TODO: Assemble Python into code variable.
- var code = dropdown_name;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField("")
- .appendField(Blockly.Msg.digital_set);
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"],
- ["CLK", "PC0"],
- ["CS0", "PC1"],
- ["MOSI", "PC2"],
- ["MISO", "PC3"],
- ["TXD", "PG6"],
- ["RXD", "PG7"],
- ["P0", "10"],
- ["P1", "11"],
- ["P2", "12"],
- ["P3", "13"],
- ["A0", "14"],
- ["A1", "15"],
- ["A2", "16"],
- ["A3", "17"]
- // ["3", "231"],
- // ["4", "232"],
- // ["5", "237"],
- // ["6", "238"]
- ]), "digital_set_type_gpio").appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_set_as);
- this.appendValueInput("high_low")
- .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('digital_set_type');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function (block) {
- var high_low = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC).slice(1, 2);
- var timer_type = block.getFieldValue('digital_set_type');
- let value_name = block.getFieldValue('digital_set_type_gpio');
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_os'] = `import os`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ''
- if (value_name == 2 || value_name == 1) {
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable('MATHOUT' + value_name, `MATHOUT${value_name} = multiFuncGpio(${value_name - 1},3)`, true);
- code = `MATHOUT${value_name}.digitalWrite(${high_low})
- `
- } else if (value_name == "PG6" || value_name == "PG7" || value_name == "PC0" || value_name == "PC1" || value_name == "PC2" || value_name == "PC3") {
- Blockly.Python.addFunction("pinMode", `def pinMode(pin,mode):
- os.system(f"echo {pin} {mode} > /sys/kernel/debug/sunxi_pinctrl/function")
- `)
- Blockly.Python.addFunction("digitalWrite", `def digitalWrite(pin,level):
- if pin == "PC1" or pin == "PC0" or pin == "PC2" or pin == "PC3":
- os.system(f"echo {pin} 1 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system(f"echo {pin} {level} > /sys/kernel/debug/sunxi_pinctrl/data")
- `)
- Blockly.Python.addFunction("digitalRead", `def digitalRead(pin):
- if pin == "PC1" or pin == "PC0" or pin == "PC2" or pin == "PC3":
- os.system("echo {pin} 2 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system(f"echo {pin} > /sys/kernel/debug/sunxi_pinctrl/sunxi_pin")
- return int(os.popen("cat /sys/kernel/debug/sunxi_pinctrl/data").read()[-2:])
- `)
- Blockly.Python.addFunction("setToSpi", `def setToSpi():
- os.system("echo PC0 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC1 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC2 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC3 4 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PC0 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC1 1 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC2 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- os.system("echo PC3 0 > /sys/kernel/debug/sunxi_pinctrl/pull")
- `)
- Blockly.Python.addFunction("setToSerial", `def setToSerial():
- os.system("echo PG6 5 > /sys/kernel/debug/sunxi_pinctrl/function")
- os.system("echo PG7 5 > /sys/kernel/debug/sunxi_pinctrl/function")
- `)
- high_low = 1-high_low
- if(value_name == "PG6" || value_name == "PG7"){
- Blockly.Python.addVariable(`pinModeOut${value_name}`, `pinMode("${value_name}","1")`, false)
- }else{
- Blockly.Python.addVariable(`pinModeOut${value_name}`, `pinMode("${value_name}","0")`, false)
- }
-
- code = `digitalWrite("${value_name}","${high_low}")
- `
- } else {
- let gpioNum = value_name - 10
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `extGpio = extGpio()`;
- Blockly.Python.addVariable('ExtGpio' + value_name, `extGpio.pinMode(${gpioNum},1)`, true);
- code = `extGpio.digitalWrite(${gpioNum},${high_low})\n`
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_io_out_pwm'] = {
- init: function () {
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_set_type")
- .appendField(Blockly.Msg.digital_set) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_set_type")
- .appendField(Blockly.Msg.digital_set);
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- ['6', '6'],
- ['7', '7'],
- ['8', '8']
- ]), "digital_set_type_gpio").appendField(Blockly.Msg.OUT_PWM);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('digital_set_type');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['esp32_main_controller_io_out_pwm'] = function (block) {
- var value_name1 = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_name2 = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC).slice(1, 2);
- var timer_type = block.getFieldValue('digital_set_type');
- let digital_set_type_gpio = block.getFieldValue('digital_set_type_gpio');
- var code = "";
- if (timer_type == 1) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_main_controller_io_out_pwm'] = '' +
- 'from machine import Pin\n' +
- '';
- let variable = '_PIN_OUT_' + value_name1 + ' = Pin(' + value_name1 + ', Pin.OUT)\n'
- Blockly.Python.addVariable('_PIN_OUT_' + value_name1, variable, true);
- code = '_PIN_OUT_' + value_name1 + '.value' + value_name2 + '\n';
- } else if (timer_type == 2) {
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['import_main_controller_io_out_pwm'] = '' +
- 'from Maix import pwm\n' +
- 'def numberMap(value, leftMin, leftMax, rightMin, rightMax):\n' +
- ' leftSpan = leftMax - leftMin\n' +
- ' rightSpan = rightMax - rightMin\n' +
- ' valueScaled = float(value - leftMin) / float(leftSpan)\n' +
- ' return rightMin + (valueScaled * rightSpan)\n' +
- '';
- // let variable = 'fm.register(' + value_name1 + ',fm.fpioa.GPIOHS' + value_name1 + ')\n' +
- // '_set_ai_ext_pin' + value_name1 + ' = GPIO(GPIO.GPIOHS' + value_name1 + ', GPIO.OUT)\n'
- // Blockly.Python.addVariable('_set_ai_ext_pin' + value_name1, variable, true);
- code = 'pwm6.PWM(' + digital_set_type_gpio + ')\n' + // 配置PWM引脚,可用引脚:6、7、8
- 'pwm6.export()\n' +
- 'pwm6.period = 20000000 \n' + //表示 pwm 的周期,单位 ns
- 'pwm_6_value=0\n' +
- 'pwm_6.duty_cycle = numberMap(pwm_6_value,0,1023,0,20000000)\n' + // # 表示占空比,单位 ns,map函数
- 'pwm6.duty_cycle = 500000\n' +
- 'pwm6.enable = True'; // 表示是否使能 pwm
- }
- return code;
- }
- Blockly.Blocks['esp32_main_controller_io_from_analog_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.analog_get)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"],
- ["A0", "4"],
- ["A1", "5"],
- ["A2", "6"],
- ["A3", "7"]
- ]), "gpio");
- this.appendDummyInput()
- .appendField(Blockly.Msg.analog_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Io_From_Analog_Pin_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_io_from_analog_pin'] = function (block) {
- // var value_name = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_gpio = block.getFieldValue('gpio');
- // TODO: Assemble Python into code variable.
- //Blockly.Python.addVariable('_PWM_PIN_IN_' + value_name, '_PWM_PIN_IN_' + value_name + '=None', true);
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ""
- if (value_gpio == 1 || value_gpio == 2) {
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable('_PWN_gpio_' + value_gpio, `_PWN_gpio_${value_gpio} = multiFuncGpio(${value_gpio - 1},4)`, true);
- code = `_PWN_gpio_${value_gpio}.analogRead()`;
- } else {
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `InputGpio = extGpio()`;
- Blockly.Python.addVariable('InputGpio' + value_gpio, `InputGpio.pinMode(${value_gpio},0)`, true);
- code = `InputGpio.analogRead(${value_gpio})`;
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_io_set_analog_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_set)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"],
- ["A0", "4"],
- ["A1", "5"],
- ["A2", "6"],
- ["A3", "7"]
- // ["7", "7"],
- // ["8", "8"],
- // ["9", "9"],
- // ["10", "10"],
- // ["11", "11"],
- // ["12", "12"]
- ]), "gpio");
- this.appendValueInput("pwm")
- .appendField(Blockly.Msg.set_pwm)
- .setCheck(null);
- this.appendDummyInput().appendField(Blockly.Msg.esp32_expand_io_set_analog_pin_digital_set_af)
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('LED');
- var TOOLTIPS = {
- 'LED': Blockly.Msg.Esp32_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_set_analog_pin'] = function (block) {
- var value_name1 = block.getFieldValue('gpio');
- var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ""
- if (value_name1 == 1 || value_name1 == 2) {
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable('PWNOUT_' + value_name1, `PWNOUT_${value_name1} = multiFuncGpio(${value_name1 - 1},5)`, true);
- code = `PWNOUT_${value_name1}.analogWrite(${value_name2})\n`;
- } else {
- let num = value_name1 - 4
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
- Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `InputGpio = extGpio()`;
- Blockly.Python.addVariable('InputGpio' + value_name1, `InputGpio.pinMode(${value_name1},1)`, true);
- code = `InputGpio.analogWrite(${num},${value_name2})\n`;
- }
- return code;
- };
- Blockly.Blocks['ai_main_controller_io_set_analog_pin'] = {
- init: function () {
- var _en = true;
- for (var i = 0; i < ids.length; i++) {
- if (!ids[i]) {
- _en = false;
- }
- }
- this.disabled = ids.length > 11 && _en ? true : false;
- this.appendValueInput("analog_pin_input")
- .appendField(Blockly.Msg.analog_set_ai)
- .setCheck(null);
- // this.appendValueInput("frequency")
- // .appendField(Blockly.Msg.analog_set_freq)
- // .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.control)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.led, "LED"]
- ]), "LED");
- this.appendValueInput("pwm")
- .appendField(Blockly.Msg.set_pwm)
- .setCheck(null);
- // this.appendValueInput("timer")
- // .appendField(Blockly.Msg.analog_set_timer)
- // .setCheck(null);
- // this.appendValueInput("channel")
- // .appendField(Blockly.Msg.analog_set_timer_channel)
- // .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('LED');
- var TOOLTIPS = {
- 'LED': Blockly.Msg.Ai_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- var ids = [];
- Blockly.Python['ai_main_controller_io_set_analog_pin'] = function (block) {
- var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
- var str = '';
- var j = 0;
- var returnvalue = '';
- for (var i = 0; i < ids.length; i++) {
- if (ids[i] && !Blockly.getMainWorkspace().getBlockById(ids[i].id)) {
- ids[i] = null;
- }
- }
- for (var i = 0; i < ids.length; i++) {
- if (ids[i] && ids[i].id == block.id) {
- j = i;
- break;
- } else {
- if (j == 0) {
- if (!ids[i]) {
- j = i;
- } else if (i == ids.length - 1) {
- j = ids.length;
- }
- }
- }
- }
- if (!ids[j]) {
- ids[j] = { id: block.id, index: j };
- }
- for (var i = 0; i < ids.length; i++) {
- if (ids[i]) {
- Blockly.Python.addVariable('_pwm_gpio_tim_' + ids[i].index, '', true);
- Blockly.Python.addVariable('_pwm_gpio_control_' + ids[i].index, '', true);
- str += '_pwm_gpio_tim_' + ids[i].index + ' = Timer(_io_timer_use[' + ids[i].index + '][0],_io_timer_use[' + ids[i].index + '][1], mode=Timer.MODE_PWM)\n';
- str += '_pwm_gpio_control_' + ids[i].index + ' = PWM(_pwm_gpio_tim_' + ids[i].index + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- }
- }
- returnvalue += '_pwm_gpio_control_' + ids[j].index + '.duty(int(' + value_name2 + '/10.23))\n';
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
- 'from machine import Pin,PWM,Timer\n' +
- '\n' +
- '_io_timer_use = [[Timer.TIMER0, Timer.CHANNEL0],[Timer.TIMER0, Timer.CHANNEL1],[Timer.TIMER0, Timer.CHANNEL2],[Timer.TIMER1, Timer.CHANNEL0],[Timer.TIMER1, Timer.CHANNEL1],[Timer.TIMER1, Timer.CHANNEL2],[Timer.TIMER2, Timer.CHANNEL0],[Timer.TIMER2, Timer.CHANNEL1],[Timer.TIMER2, Timer.CHANNEL2]]\n' + str;
- return returnvalue;
- // var module_type = block.getFieldValue('digital_set_type');
- // var str = '';
- // var ss = '';
- // if (counts.length != 0) {
- // if (ids.indexOf(block.id) == -1) {
- // ids.push(block.id);
- // count = count + 1;
- // counts.push(count);
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
- // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // else {
- // for (var i = 0; i < counts.length; i++) {
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + counts[i], '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + counts[i], '', true);
- // str += '_pwm_gpio_tim_' + counts[i] + ' = Timer(_io_timer_use[' + counts[i] + '][0],_io_timer_use[' + counts[i] + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + counts[i] + ' = PWM(_pwm_gpio_tim_' + counts[i] + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + counts[i] + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // }
- // }
- // else {
- // if (ids.indexOf(block.id) == -1) {
- // ids.push(block.id);
- // counts.push(count);
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
- // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // }
- // // TODO: Assemble Python into code variable.
- // Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
- // 'from machine import Pin,PWM,Timer\n' +
- // '\n' +
- // '_io_timer_use = [[Timer.TIMER0, Timer.CHANNEL0],[Timer.TIMER0, Timer.CHANNEL1],[Timer.TIMER0, Timer.CHANNEL2],[Timer.TIMER1, Timer.CHANNEL0],[Timer.TIMER1, Timer.CHANNEL1],[Timer.TIMER1, Timer.CHANNEL2],[Timer.TIMER2, Timer.CHANNEL0],[Timer.TIMER2, Timer.CHANNEL1],[Timer.TIMER2, Timer.CHANNEL2]]\n' + str;
- // // code = ss;
- // return ss;
- };
- /*
- Button
- */
- Blockly.Blocks['esp32_main_controller_button_read_pressed'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/button_pressed.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32BUTTON)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.ESP32PRESSED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_button_read_pressed'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(_' + _type + '.value() == ' + 1 + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_button_read_released'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/button_released.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32BUTTON)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.ESP32RELEASED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_button_read_released'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(_' + _type + '.value() == ' + 0 + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_button_read_value'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_button_read_value)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.basic_button_read_value_after);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_button_read_value'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- // TODO: Assemble Python into code variable.
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '_' + _type + '.value()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Python['esp32_main_controller_button_read'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "1" ? "36" : "22";
- var _upstr = "";
- if (_type == "2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('p' + _type, 'p' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(p' + _type + '.value() == ' + (_button_state == "button_state_pressed" ? "1" : "0") + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- */
- /*
- Dialer
- */
- Blockly.Blocks['esp32_main_controller_dial_switch_read_pressed'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32DIALSWITCH)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
- [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
- [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
- ]), "switch_type")
- .appendField(Blockly.Msg.ESP32PRESSED)
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_dial_switch_read_pressed'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getFieldValue('switch_type');
- var _num = "34";
- var _upstr = "";
- var _button_type = "";
- if (_type == "switch_type_left") {
- var _num = "34";
- _button_type = "Left";
- } else if (_type == "switch_type_middle") {
- var _num = "19";
- _upstr = ", Pin.PULL_UP";
- _button_type = "Middle";
- } else if (_type == "switch_type_right") {
- var _num = "35";
- _button_type = "Right";
- }
- // Blockly.Python.addVariable("dialer_type", 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
- Blockly.Python.addVariable("pLeft", 'pLeft = Pin(38, Pin.IN)');
- Blockly.Python.addVariable("pMiddle", 'pMiddle = Pin(19, Pin.IN)');
- Blockly.Python.addVariable("pRight", 'pRight = Pin(35, Pin.IN)');
- var code = '(p' + _button_type + '.value() == 1)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_dial_switch_read_released'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32DIALSWITCH)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
- [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
- [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
- ]), "switch_type")
- .appendField(Blockly.Msg.ESP32RELEASED)
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_dial_switch_read_released'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getFieldValue('switch_type');
- var _state = block.getFieldValue('switch_state');
- var _num = "34";
- var _upstr = "";
- var _button_type = "";
- if (_type == "switch_type_left") {
- var _num = "34";
- _button_type = "Left";
- } else if (_type == "switch_type_middle") {
- var _num = "19";
- _upstr = ", Pin.PULL_UP";
- _button_type = "Middle";
- } else if (_type == "switch_type_right") {
- var _num = "35";
- _button_type = "Right";
- }
- Blockly.Python.addVariable('p' + _button_type, 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
- var code = '(p' + _button_type + '.value() == 0)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- LED
- */
- Blockly.Blocks['esp32_main_controller_set_led_turnon'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/led_on.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
- this.appendValueInput("led_type")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNON);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnon_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_turnon'] = function (block) {
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
- var _state = block.getFieldValue('led_state');
- var _num = "2";
- if (_type == "1") {
- var _num = "2";
- } else if (_type == "2") {
- var _num = "12";
- } else if (_type == "3") {
- var _num = "5";
- } else if (_type == "4") {
- var _num = "13";
- } else if (_type == "5") {
- var _num = "14";
- } else if (_type == "6") {
- var _num = "15";
- } else if (_type == "7") {
- var _num = "18";
- } else if (_type == "8") {
- var _num = "19";
- } else if (_type == "9") {
- var _num = "23";
- } else {
- var _num = "ERROR";
- }
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- var code = '_led_light(True,' + _type + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_turnoff'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/led_off.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
- this.appendValueInput("led_type")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNOFF);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnoff_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_turnoff'] = function (block) {
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
- var _state = block.getFieldValue('led_state');
- var _num = "2";
- if (_type == "1") {
- var _num = "2";
- } else if (_type == "2") {
- var _num = "12";
- } else if (_type == "3") {
- var _num = "5";
- } else if (_type == "4") {
- var _num = "13";
- } else if (_type == "5") {
- var _num = "14";
- } else if (_type == "6") {
- var _num = "15";
- } else if (_type == "7") {
- var _num = "18";
- } else if (_type == "8") {
- var _num = "19";
- } else if (_type == "9") {
- var _num = "23";
- } else {
- var _num = "ERROR";
- }
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- var code = '_led_light(False,' + _type + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_drawpixel_on'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTUP + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
- this.appendValueInput("x")
- .setCheck(null)
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_On_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_drawpixel_on'] = function (block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- Blockly.Python.addSetup('drawpixel_setup', '' +
- 'def _led_draw_pixel(state, x, y):\n' +
- ' real_pin = (3*y-3+x)\n' +
- ' _led_light(state,real_pin)\n' +
- '\n')
- var code = '_led_draw_pixel(True,' + value_x + ',' + value_y + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_drawpixel_off'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTOFF + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
- this.appendValueInput("x")
- .setCheck(null)
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_Off_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_drawpixel_off'] = function (block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- Blockly.Python.addSetup('drawpixel_setup', '' +
- 'def _led_draw_pixel(state, x, y):\n' +
- ' real_pin = (3*y-3+x)\n' +
- ' _led_light(state,real_pin)\n' +
- '\n')
- var code = '_led_draw_pixel(False,' + value_x + ',' + value_y + ')\n';
- return code;
- };
- /*
- BUZZER
- */
- Blockly.Blocks['esp32_main_controller_set_buzzer_init'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/buzzer.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField( /*Blockly.Msg.ESP32TURNOBAF*/);
- //.appendField(new Blockly.FieldNumber(31, 31, 65535, 1), "buzzer_frequency");
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_buzzer_set_init)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "0"],
- ["S2", "1"]
- ]), "notes")
- this.setInputsInline(true);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Buzzer_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_buzzer_init'] = function (block) {
- var notes = block.getFieldValue('notes');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable('BUZZER' + notes, `BUZZER${notes} = multiFuncGpio(${notes},6)`, true);
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_buzzer'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/buzzer.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField( /*Blockly.Msg.ESP32TURNOBAF*/);
- //.appendField(new Blockly.FieldNumber(31, 31, 65535, 1), "buzzer_frequency");
- this.appendDummyInput().appendField(new Blockly.FieldDropdown([
- ["S1", "0"],
- ["S2", "1"]
- ]), "notes")
- .appendField(Blockly.Msg.basic_buzzer_set)
- .appendField(new Blockly.FieldNumber(31, 20, 12000), 'esp32_var_num');
- this.setInputsInline(true);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Buzzer_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_buzzer'] = function (block) {
- var esp32_var_num = block.getFieldValue("esp32_var_num");
- var notes = block.getFieldValue('notes');
- var code = `BUZZER${notes}.beep(${esp32_var_num})
- `;
- return code;
- };
- Blockly.Blocks['esp32_main_controller_turn_off_buzzer'] = {
- init: function () {
- this.appendDummyInput().appendField(new Blockly.FieldDropdown([
- ["S1", "0"],
- ["S2", "1"]
- ]), "notes").appendField(Blockly.Msg.ESP32TURNOB);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Turn_Off_Buzzer_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_turn_off_buzzer'] = function (block) {
- var notes = block.getFieldValue('notes');
- var code = `BUZZER${notes}.beep(0)
- `
- return code;
- //return [code, Blockly.Python.ORDER_FUNCTION_CALL];
- };
- Blockly.Blocks['esp32_main_controller_turn_off_buzzer_music'] = {
- init: function () {
- this.appendDummyInput().appendField(new Blockly.FieldDropdown([
- ["S1", "0"],
- ["S2", "1"]
- ]), "type")
- .appendField(Blockly.Msg.esp32_main_controller_turn_off_buzzer_music_lite)
- .appendField(new Blockly.FieldDropdown([
- ["C2", "65"],
- ["D2", "73"],
- ["E2", "82"],
- ["F2", "87"],
- ["G2", "98"],
- ["A2", "110"],
- ["B2", "123"],
- ["C3", "130"],
- ["D3", "146"],
- ["E3", "164"],
- ["F3", "174"],
- ["G3", "196"],
- ["A3", "220"],
- ["B3", "246"],
- ["C4", "261"],
- ["D4", "293"],
- ["E4", "329"],
- ["F4", "349"],
- ["G4", "392"],
- ["A4", "440"],
- ["B4", "493"],
- ["C5", "523"],
- ["D5", "587"],
- ["E5", "659"],
- ["F5", "698"],
- ["G5", "784"],
- ["A5", "880"],
- ["B5", "987"],
- ["C6", "1046"],
- ["D6", "1174"],
- ["E6", "1318"],
- ["F6", "1396"],
- ["G6", "1568"],
- ["A6", "1760"],
- ["B6", "1975"],
- ["C7", "2093"],
- ["D7", "2349"],
- ["E7", "2637"],
- ["F7", "2793"],
- ["G7", "3136"],
- ["A7", "3520"],
- ["B7", "3951"],
- ["C8", "4186"],
- ["D8", "4698"],
- ["E8", "5274"],
- ["F8", "5587"],
- ["G8", "6272"],
- ["A8", "7040"],
- ["B8", "7902"]
- ]), "notes")
- .appendField(Blockly.Msg.esp32_main_controller_turn_off_buzzer_music_lite_2);
- this.appendValueInput("beat")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_main_controller_turn_off_buzzer_music_lite_3);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.esp32_main_controller_turn_off_buzzer_music_lite_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_turn_off_buzzer_music'] = function (block) {
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- var beat = Blockly.Python.valueToCode(block, 'beat', Blockly.Python.ORDER_ATOMIC).replace("(", "").replace(")", "");
- var type = block.getFieldValue('type');
- var notes = block.getFieldValue('notes');
- // TODO: Assemble Python into code variable.
- // TODO: Assemble Python into code variable.
- var code = `BUZZER${type}.beep(${notes})
- time.sleep(${beat})
- `
- return code;
- };
- Blockly.Blocks['esp32_main_controller_buzzer_set_beat_lite'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- ["1/2", "0.5"],
- ["1", "1"],
- ["1/4", "0.25"],
- ["1/8", "0.125"],
- ["1/16", "0.063"]
- ]), "beat");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('beat');
- var TOOLTIPS = {
- "1": "%1".replace("%1", "1"),
- "0.5": "%1".replace("%1", "1/2"),
- "0.25": "%1".replace("%1", "1/4"),
- "0.125": "%1".replace("%1", "1/8"),
- "0.063": "%1".replace("%1", "1/16")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_buzzer_set_beat_lite'] = function (block) {
- var value_name1 = block.getFieldValue('beat');
- var code = value_name1;
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- TOUCH AREA
- */
- Blockly.Blocks['esp32_main_controller_touch_read_touched'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/touched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TA)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_TOUCHED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_touched'] = function (block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") < 100';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_touch_read_untouched'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/untouched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TA)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_UNTOUCHED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_untouched'] = function (block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") >= 100';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_touch_read_value'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_READ)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_READ_VALUE);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_value'] = function (block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '")';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- Env Sensor: BME280
- */
- Blockly.Blocks['esp32_main_controller_get_environmental_value'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_temperature, "temperature"],
- [Blockly.Msg.basic_humidity, "humidity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('env_get_type');
- var TOOLTIPS = {
- 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
- 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_get_environmental_value'] = function (block) {
- var _type_selected = block.getFieldValue('env_get_type');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_AHT20'] = `from CocoPi import AHT20`
- // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_AHT20_object'] = `aht20 = AHT20(2) `
- Blockly.Python.addVariable('_BME', '');
- let code = ''
- if (_type_selected == 'temperature') {
- code = `aht20.get_temperature()`
- } else if (_type_selected == 'humidity') {
- code = `aht20.get_humidity()`;
- } else {
- code = _type_selected;
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- Motion Sensor: QMI8658
- */
- Blockly.Blocks['esp32_main_controller_motion_init'] = {
- init: function () {
- //this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/motion_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_use);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_init'] = function (block) {
- // Blockly.Python.addVariable('_SENSOR_MPU_PINS', '');
- Blockly.Python.definitions_['import_QMI8658'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import QMI8658\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- var code = '' +
- '_MPU = QMI8658.accel(_SENSOR_MPU_PINS)\n' +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_when_shaking'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_shaking);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_shaking'] = function (block) {
- Blockly.Python.definitions_['shaking_def'] = '' +
- 'def _CHECK_SHAKING():\n' +
- ' if _MPU_SHAKING_OFFSET < 0 and _SSTATE == 0:\n' +
- ' _SSTATE = 1\n' +
- ' return True\n' +
- ' elif _MPU_SHAKING_OFFSET > 0 and _SSTATE == 1:\n' +
- ' _SSTATE = 0\n' +
- ' return False\n' +
- '';
- var code = '(_CHECK_SHAKING() == True)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_when_shaking_STATEMENT'] = {
- init: function () {
- this.appendDummyInput()
- .appendField()
- .appendField(Blockly.Msg.basic_shaking_sensitivity)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_shaking_sensitivity_high, "high"],
- [Blockly.Msg.basic_shaking_sensitivity_middle, "middle"],
- [Blockly.Msg.basic_shaking_sensitivity_low, "low"]
- ]), "sensitivity")
- .appendField(Blockly.Msg.basic_when_shaking_when_module)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_shaking_when_is_in, "True"],
- [Blockly.Msg.basic_when_shaking_when_is_not_in, "False"]
- ]), "status")
- .appendField(Blockly.Msg.basic_when_shaking_when_shaking);
- this.appendStatementInput("input")
- .setCheck(null)
- .appendField(Blockly.Msg.basic_motion_statement_exec);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('status');
- var TOOLTIPS = {
- 'True': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_in),
- 'False': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_not_in)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_shaking_STATEMENT'] = function (block) {
- var dropdown_status = block.getFieldValue('status');
- var sensitivity = block.getFieldValue('sensitivity');
- var statements_input = Blockly.Python.statementToCode(block, 'input');
- var temp = 0;
- if (sensitivity == "high") {
- temp = 99;
- }
- else if (sensitivity == "middle") {
- temp = 23;
- }
- else {
- temp = 1;
- }
- Blockly.Python.definitions_['import_QMI8658'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import QMI8658\n' +
- '\n' +
- 'mpu_lib_version_is_latest = "QMI8658" in dir(QMI8658)\n' +
- 'if mpu_lib_version_is_latest == True:\n' +
- ' mpui2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
- ' QMI8658 = QMI8658.QMI8658(mpui2c)\n' +
- ' mpucal = QMI8658.calibrate()\n' +
- 'elif mpu_lib_version_is_latest == False:\n' +
- ' _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- ' _MPU = QMI8658.accel(_SENSOR_MPU_PINS)\n' +
- ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- ' _MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- ' _MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- ' _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- ' _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- ' _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '\n' +
- '';
- Blockly.Python.definitions_['shaking_def'] = '' +
- 'def _CHECK_SHAKING():\n' +
- ' _MPU = QMI8658.accel(_SENSOR_MPU_PINS)\n' +
- ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- ' _SSTATE = 0\n' +
- ' if _MPU_SHAKING_OFFSET < 0:\n' +
- ' _SSTATE = 1\n' +
- ' return True\n' +
- ' elif _MPU_SHAKING_OFFSET > 0:\n' +
- ' _SSTATE = 0\n' +
- ' return False\n' +
- '';
- var code = '' +
- 'if mpu_lib_version_is_latest == True and (QMI8658.is_shaking(mpucal,' + temp + ') == ' + dropdown_status + '):\n' +
- '' + statements_input + '\n' +
- 'elif mpu_lib_version_is_latest == False and (_CHECK_SHAKING() == ' + dropdown_status + '):\n' +
- ' _MPU = QMI8658.accel(_SENSOR_MPU_PINS)\n' +
- ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '' + statements_input + '\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_when_tilting'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_tilting_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_tilting_get_left, "L"],
- [Blockly.Msg.basic_when_tilting_get_right, "R"],
- [Blockly.Msg.basic_when_tilting_get_forward, "F"],
- [Blockly.Msg.basic_when_tilting_get_backward, "B"]
- ]), "tilt_type")
- .appendField(Blockly.Msg.basic_when_tilting_get_when);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_tilting'] = function (block) {
- var _type_selected = block.getFieldValue('tilt_type');
- Blockly.Python.definitions_['tilting_def'] = '' +
- 'def _CHECK_TILT_LR():\n' +
- ' if _MPU_OFFSET_TILT_X < 160 and _MPU_OFFSET_TILT_X > 0:\n' +
- ' return "R"\n' +
- ' elif _MPU_OFFSET_TILT_X > 200 and _MPU_OFFSET_TILT_X < 360:\n' +
- ' return "L"\n' +
- ' else:\n' +
- ' return None\n' +
- '\n' +
- 'def _CHECK_TILT_FB():\n' +
- ' if _MPU_OFFSET_TILT_Y < 160 and _MPU_OFFSET_TILT_Y > 0:\n' +
- ' return "F"\n' +
- ' elif _MPU_OFFSET_TILT_Y > 200 and _MPU_OFFSET_TILT_Y < 360:\n' +
- ' return "B"\n' +
- ' else:\n' +
- ' return None\n' +
- '';
- if (_type_selected == "L" || _type_selected == "R") {
- _check_function = '(_CHECK_TILT_LR() == "';
- } else if (_type_selected == "F" || _type_selected == "B") {
- _check_function = '(_CHECK_TILT_FB() == "';
- }
- var code = _check_function + _type_selected + '")';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_when_tilting_STATEMENT'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_tilting_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_tilting_get_left, "L"],
- [Blockly.Msg.basic_when_tilting_get_right, "R"],
- [Blockly.Msg.basic_when_tilting_get_forward, "F"],
- [Blockly.Msg.basic_when_tilting_get_backward, "B"],
- // [Blockly.Msg.basic_when_tilting_get_left_and_forward, "LF"],
- // [Blockly.Msg.basic_when_tilting_get_left_and_backward, "LB"],
- // [Blockly.Msg.basic_when_tilting_get_right_and_forward, "RF"],
- // [Blockly.Msg.basic_when_tilting_get_right_and_backward, "RB"],
- // [Blockly.Msg.basic_when_tilting_not_tilting, "N"]
- ]), "tilt_type")
- .appendField(Blockly.Msg.basic_when_tilting_get_when);
- this.appendStatementInput("input")
- .setCheck(null)
- .appendField(Blockly.Msg.basic_motion_statement_exec);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null)
- this.setColour("#5fcd8e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('tilt_type');
- var TOOLTIPS = {
- 'L': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left),
- 'R': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right),
- 'F': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_forward),
- 'B': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_backward),
- 'LF': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left_and_forward),
- 'LB': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left_and_backward),
- 'RF': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right_and_forward),
- 'RB': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right_and_backward),
- 'N': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_not_tilting)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_tilting_STATEMENT'] = function (block) {
- var _type_selected = block.getFieldValue('tilt_type');
- var statements_input = Blockly.Python.statementToCode(block, 'input');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_QMI8658'] = `from CocoPi import QMI8658`
- Blockly.Python.definitions_['v831_QMI8658_import_path'] = `
- #初始化设置
- qmi8658=QMI8658()
- #校准
- qmi8658.calibrate()
- #标定初始方位
- initData={}
- initData["AcX"]=qmi8658.get_accel(10)["AcX"]
- initData["AcY"]=qmi8658.get_accel(10)["AcY"]
- initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
- initData["GyX"]=qmi8658.get_accel(10)["GyX"]
- initData["GyY"]=qmi8658.get_accel(10)["GyY"]
- initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
- `;
- // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
- var code = ``;
- if (_type_selected == "L") {
- code = `if round(qmi8658.getPitchYawRollGxGyGz(initData)[0],2) < -15:
- ${statements_input}`
- } else if (_type_selected == "R") {
- code = `if round(qmi8658.getPitchYawRollGxGyGz(initData)[0],2) > 15:
- ${statements_input}`
- } else if (_type_selected == "B") {
- code = `if round(qmi8658.getPitchYawRollGxGyGz(initData)[1],2) < -15:
- ${statements_input}`
- } else if (_type_selected == "F") {
- code = `if round(qmi8658.getPitchYawRollGxGyGz(initData)[1],2) > 15:
- ${statements_input}`
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_acceleration'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_get_along)
- .appendField(new Blockly.FieldDropdown([
- ["X", "4"],
- ["Y", "3"],
- ["Z", "5"]
- ]), "accel_type")
- .appendField(Blockly.Msg.basic_motion_accel);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.isUnit)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.cancUnit, "N"],
- // [Blockly.Msg.confUnit, "Y"]
- // ]), "isUnit");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('accel_type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'X'),
- 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Y'),
- 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Z')
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_acceleration'] = function (block) {
- var _type_selected = block.getFieldValue('accel_type');
- // var is_unit = block.getFieldValue('isUnit');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_QMI8658'] = `from CocoPi import QMI8658`
- Blockly.Python.definitions_['v831_QMI8658_import_path'] = `
- #初始化设置
- qmi8658=QMI8658()
- #校准
- qmi8658.calibrate()
- #标定初始方位
- initData={}
- initData["AcX"]=qmi8658.get_accel(10)["AcX"]
- initData["AcY"]=qmi8658.get_accel(10)["AcY"]
- initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
- initData["GyX"]=qmi8658.get_accel(10)["GyX"]
- initData["GyY"]=qmi8658.get_accel(10)["GyY"]
- initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
- `;
- // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
- var code = `round(qmi8658.getPitchYawRollGxGyGz(initData)[${_type_selected}],2)`;
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_rotation_measurement'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_get_along)
- .appendField(new Blockly.FieldDropdown([
- ["X", "4"],
- ["Y", "3"],
- ["Z", "5"]
- ]), "accel_type")
- .appendField(Blockly.Msg.basic_motion_rotation_measurement);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.isUnit)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.cancUnit, "N"],
- // [Blockly.Msg.confUnit, "Y"]
- // ]), "isUnit");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('accel_type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Rotation_Measurement_TOOLTIP.replace('%1', 'X'),
- 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Rotation_Measurement_TOOLTIP.replace('%1', 'Y'),
- 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Rotation_Measurement_TOOLTIP.replace('%1', 'Z')
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_rotation_measurement'] = function (block) {
- var _type_selected = block.getFieldValue('accel_type');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_QMI8658'] = `from CocoPi import QMI8658`
- Blockly.Python.definitions_['v831_QMI8658_import_path'] = `
- #初始化设置
- qmi8658=QMI8658()
- #校准
- qmi8658.calibrate()
- #标定初始方位
- initData={}
- initData["AcX"]=qmi8658.get_accel(10)["AcX"]
- initData["AcY"]=qmi8658.get_accel(10)["AcY"]
- initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
- initData["GyX"]=qmi8658.get_accel(10)["GyX"]
- initData["GyY"]=qmi8658.get_accel(10)["GyY"]
- initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
- `;
- // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
- var code = `round(qmi8658.getPitchYawRollGxGyGz(initData)[${_type_selected}],2)`;
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_tilt_angle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_get_along)
- .appendField(new Blockly.FieldDropdown([
- ["X", "1"],
- ["Y", "0"],
- // ["Z", "2"]
- ]), "tilt_angle_type")
- .appendField(Blockly.Msg.basic_motion_angle);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.isUnit)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.cancUnit, "N"],
- // [Blockly.Msg.confUnit, "Y"]
- // ]), "isUnit");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('tilt_angle_type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'X'),
- 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Y')
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_tilt_angle'] = function (block) {
- var _type_selected = block.getFieldValue('tilt_angle_type');
- // var is_unit = block.getFieldValue('isUnit');
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_QMI8658'] = `from CocoPi import QMI8658`
- Blockly.Python.definitions_['v831_QMI8658_import_path'] = `
- #初始化设置
- qmi8658=QMI8658()
- #校准
- qmi8658.calibrate()
- #标定初始方位
- initData={}
- initData["AcX"]=qmi8658.get_accel(10)["AcX"]
- initData["AcY"]=qmi8658.get_accel(10)["AcY"]
- initData["AcZ"]=qmi8658.get_accel(10)["AcZ"]
- initData["GyX"]=qmi8658.get_accel(10)["GyX"]
- initData["GyY"]=qmi8658.get_accel(10)["GyY"]
- initData["GyZ"]=qmi8658.get_accel(10)["GyZ"]
- `
- var code = `round(qmi8658.getPitchYawRollGxGyGz(initData)[${_type_selected}],2)`
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- LIGHT & SOUND
- */
- Blockly.Blocks['esp32_main_controller_get_light'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/light_sensor.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_light_get);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Light_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_light'] = function (block) {
- Blockly.Python.definitions_['v831_import_LIGHTINTENSITY'] = `class v83x_ADC():
- def __init__(self, addr=b"0x05070080") -> None:
- self.addr = addr
- self.path = "/sys/class/sunxi_dump/dump"
- self.file = open(self.path, "wb+")
- self.last = self.value()
- def __del__(self):
- try:
- if self.file:
- self.file.close()
- del self.file
- except Exception as e:
- pass
- def value(self):
- self.file.write(b"0x05070080")
- self.file.seek(0)
- return int(self.file.read()[:-1], 16)
- v831_adc0 = v83x_ADC()
- `
- var code = 'v831_adc0.value()';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /* time */
- var ESP32_TIME_COLOR = "#fabe23";
- Blockly.Blocks['esp32_main_controller_time_timer_init'] = {
- init: function () {
- this.appendDummyInput().appendField(Blockly.Msg.time_init_timer);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_init'] = function (block) {
- var timer_type = block.getFieldValue('type');
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['counter_start_variable'] = `counter_start = 0
- def counter_start_fun():
- global counter_start
- counter_start = time.perf_counter()
- `;
- var code = `counter_start_fun()
- `
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_timer_get_current'] = {
- init: function () {
- this.appendDummyInput().appendField(Blockly.Msg.time_timer_get_time);
- this.setOutput(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_get_current'] = function (block) {
- var timer_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['counter_end'] = `def getcounterEndStart(newDate):
- global counter_start
- return newDate - counter_start
- `
- // '' +
- // 'counter_end=time.perf_counter()\n' +
- // '\n';
- var code = `getcounterEndStart(time.perf_counter())`;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_time_timer_clear'] = {
- init: function () {
- this.appendDummyInput().appendField(Blockly.Msg.time_timer_clear);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_clear'] = function (block) {
- var timer_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- var code = `del counter_start
- `;
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_period_timer'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_timer_periodically);
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_timer_every)
- .appendField(new Blockly.FieldTextInput("500"), "period_timer_count")
- .appendField(Blockly.Msg.time_timer_ms);
- this.appendStatementInput("exec_period_timer")
- .setCheck(null)
- .appendField(Blockly.Msg.time_execution);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', "CocoPi"));
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_period_timer'] = function (block) {
- var text_period_timer_count = block.getFieldValue('period_timer_count') * 0.001;
- var statements_name = Blockly.Python.statementToCode(block, 'exec_period_timer');
- Blockly.Python.addVariable("STOPTHREAD", "STOPTHREAD = False", true)
- Blockly.Python.definitions_['v831_import_threading'] = `import threading`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- let allBlocks = block.workspace.getAllBlocks();
- let global = ""
- // console.log('allBlocks',allBlocks)
- try {
- global = allBlocks[0].workspace.variableList.toString()
- }
- catch (e) {
- console.log(e)
- }
- Blockly.Python.definitions_['v831_import_thread_calsss'] = `def period_timer_count(n: int):
- global STOPTHREAD
- while True:
- if STOPTHREAD:
- break
- time.sleep(${text_period_timer_count})
- thread_calsss_fun_timer()
- `
- Blockly.Python.definitions_['v831_import_thread_calsss_fun'] = `def thread_calsss_fun_timer():
- global ${global}
- ${statements_name}
- `
- var code = `CocoPiThread = threading.Thread(target=period_timer_count, args=(1,))
- CocoPiThread.start()
- `
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_period_timer_clear'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_timer_periodically_clear);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', "CocoPi"));
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_period_timer_clear'] = function (block) {
- var code = `STOPTHREAD = True
- `
- return code;
- };
- Blockly.Blocks['esp32_get_current_date'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_get_current_date_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_get_current_date_y, "Y"],
- [Blockly.Msg.time_get_current_date_m, "m"],
- [Blockly.Msg.time_get_current_date_d, "d"],
- [Blockly.Msg.time_get_current_date_hour, "H"],
- [Blockly.Msg.time_get_current_date_min, "M"],
- [Blockly.Msg.time_get_current_date_sec, "S"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(ESP32_TIME_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '0': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_y),
- '1': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_m),
- '2': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_d),
- '3': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_hour),
- '4': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_min),
- '5': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_sec)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_get_current_date'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // Blockly.Python.definitions_['v831_import_time'] = `from datetime import date`
- Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
- Blockly.Python.definitions_['v831_get_current_date'] = `def getCurrent_data(type):
- now = datetime.now()
- return now.strftime("%"+type+"")
- `
- // TODO: Assemble Python into code variable.
- var code = `getCurrent_data("${dropdown_type}")`
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- LCD
- */
- var ESP32_SCREEN_COLOR = "#5bb2d6";
- Blockly.Blocks['esp32_main_controller_lcd_rotation'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_SETROTATION)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- }
- Blockly.Python['esp32_main_controller_lcd_rotation'] = function (block) {
- var degree = block.getFieldValue("DEGREE");
- var code = "tft.rotation(" + degree + ")\n";
- // Blockly.Python.addSetup("setRotation", code);
- return code;
- }
- Blockly.Blocks['esp32_main_controller_clear_screen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_clear_screen);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_clear_screen'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'tft.fill(TFT.BLACK)\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_screen_fill'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_fill_screen);
- this.appendValueInput("COLOR")
- .setCheck(null)
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_lcd_screen_fill'] = function (block) {
- var value_lcd_rgb_value = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- if (value_lcd_rgb_value.charAt(0) != '#') {
- var code = 'tft.fill(_TFTColor' + value_lcd_rgb_value + ')\n';
- } else if (value_lcd_rgb_value.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var code = "tft.fill(_TFTColor" + d + "," + e + "," + f + ")\n";
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawpixel'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_pixel.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_pixel);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POS")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COORDINATE);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawpixel'] = function (block) {
- // var drawrectcolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + d + "," + e + "," + f + ")\n"
- } else if (color.charAt(0) != '#') {
- code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + color + ")\n"
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawline'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_LINE);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_LINE_START);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_LINE_END);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawline'] = function (block) {
- // var drawlinecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + d + "," + e + "," + f + ")\n"
- } else if (color.charAt(0) != '#') {
- code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawlinelen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.display_draw_vertical, "vline"],
- [Blockly.Msg.display_draw_horizontal, "hline"]
- ]), "rect_type")
- .appendField(Blockly.Msg.OLCD_LINE)
- this.appendValueInput("COLOR")
- .setCheck(null)
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POS")
- .setCheck(null)
- .appendField(Blockly.Msg.display_start_cord);
- this.appendValueInput("LENGTH")
- .setCheck(null)
- .appendField(Blockly.Msg.display_length);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawlinelen'] = function (block) {
- var _type = block.getFieldValue("rect_type");
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- var drawlinestart = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'LENGTH', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", _TFTColor" + color + ")\n"
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawrect'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.OLCD_FULL, "fillrect"],
- [Blockly.Msg.OLCD_CIRCLE, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.display_draw_rectangle);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COORDINATE);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_SIZE);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawrect'] = function (block) {
- // var drawlinecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var _type = block.getFieldValue("rect_type");
- var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawcircle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.OLCD_FULL, "fillcircle"],
- [Blockly.Msg.OLCD_STROKED, "circle"]
- ]), "circle_type")
- .appendField(Blockly.Msg.OLCD_CIRCLE);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_CENTER_POS);
- this.appendValueInput("RADIUS")
- .setCheck(null)
- .appendField(Blockly.Msg.OLCD_RADIUS);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawcircle'] = function (block) {
- // var drawcirclecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var circletype = block.getFieldValue('circle_type');
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = 'tft.' + circletype + '((' + position + '), ' + radius + ', _TFTColor' + color + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawqrcode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_qr.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_drawqr_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_SET_POS);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.display_drawqr_content);
- this.appendValueInput("WIDTH")
- .setCheck("Number")
- .appendField(Blockly.Msg.display_drawqr_padding);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawqrcode'] = function (block) {
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
- var code = 'tft.drawqrcode(' + '(' + position + '), ' + content + ', ' + width + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawtext'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_text);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.display_start_cord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.display_draw_text_content);
- this.appendValueInput("SIZE")
- .setCheck("Number")
- .appendField(Blockly.Msg.display_draw_text_size);
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_draw_text_return)
- .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawtext'] = function (block) {
- // var drawcirclecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
- var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = 'tft.text((' + position + '), ' + content + ', _TFTColor' + color + ', sysfont, ' + size + ', nowrap=' + box + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_rgb_value'] = {
- init: function () {
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField(Blockly.Msg.display_red);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField(Blockly.Msg.display_green);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField(Blockly.Msg.display_blue);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_rgb_value'] = function (block) {
- var r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- var original_r = parseInt(r, 10);
- var original_g = parseInt(g, 10);
- var original_b = parseInt(b, 10);
- _r = original_r.toString(16);
- _g = original_g.toString(16);
- _b = original_b.toString(16);
- if (_r.length == 1)
- _r = "0" + _r;
- if (_g.length == 1)
- _g = "0" + _g;
- if (_b.length == 1)
- _b = "0" + _b;
- // TODO: Assemble Python into code variable.
- var code = '' + r + ',' + g + ',' + b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_lcd_color_hex_to_rgb'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.COLOR)
- .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
- this.setOutput(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_color_hex_to_rgb'] = function (a) {
- a = a.getFieldValue("COLOR");
- // var d = 0
- // , e = 0
- // , f = 0;
- // try {
- // 7 == a.length && (d = a.substring(1, 3),
- // e = a.substring(3, 5),
- // f = a.substring(5, 7));
- // }
- // catch (g) { }
- var gethex = a;
- function hexToRGB(h) {
- let r = 0,
- g = 0,
- b = 0;
- // 3 digits
- if (h.length == 4) {
- r = "0x" + h[1] + h[1];
- g = "0x" + h[2] + h[2];
- b = "0x" + h[3] + h[3];
- // 6 digits
- } else if (h.length == 7) {
- r = "0x" + h[1] + h[2];
- g = "0x" + h[3] + h[4];
- b = "0x" + h[5] + h[6];
- }
- return "" + +r + "," + +g + "," + +b + "";
- }
- var _code = hexToRGB(gethex);
- //_code += "tft.fill(TFT.TFTColor(FS_COLOR))\n";
- return [_code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- WiFi
- */
- var ESP32_WIFI_COLOR = "#3062c1";
- Blockly.Blocks['esp32_main_controller_wifi_enable_hotspot_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/hotspot_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_setup_hotspot);
- this.appendValueInput("SSID")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_hotspot_ssid);
- this.appendValueInput("PASSWORD")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_hotspot_pass);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Enable_Hotspot_Mode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_enable_hotspot_mode'] = function (block) {
- var text_ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC);
- var text_password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC);
- var _code = '' +
- '_WIFI = network.WLAN(network.STA_IF)\n' +
- '_WIFI.active(False)\n' +
- '_WIFI = network.WLAN(network.AP_IF)\n' +
- '_WIFI.active(True)\n';
- Blockly.Python.definitions_['import_network'] = 'import network\n' +
- _code +
- '_WIFI.config(essid=' + text_ssid + ', password=' + text_password + ', authmode=2, channel=11, hidden=0)\n' +
- '\n';
- code = '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_wifi_connect_internet'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_connect_router);
- this.appendValueInput("ssid")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_router_ssid);
- this.appendValueInput("password")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_router_pass);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Connect_Internet_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_connect_internet'] = function (block) {
- var text_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
- var text_password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_os'] = `import os`
- // Blockly.Python.definitions_['v831_import_datetime'] = `import datetime`
- // Blockly.Python.definitions_['v831_import_ntplib'] = `import ntplib`
- Blockly.Python.definitions_['v831_wifi_getdate_noexit'] = `def getNetworkDate_noexit():
- global getDateNum
- try:
- coon = http.client.HTTPConnection("www.baidu.com")
- coon.request("GET","/")
- r = coon.getresponse()
- ts = r.getheader("date")
- GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
- BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60)
- format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
- command = "date -s "+"\\"{}\\"".format(format_time)
- os.system(command)
- getDateNum = 1
- # sys.exit()
- except:
- pass
- `
- var code = `
- WiFiSSID = ${text_ssid}
- WiFiPSD = ${text_password}
- os.system("wifi_disconnect_ap_test")
- os.system("wifi_connect_chinese_ap_test "+WiFiSSID+" "+WiFiPSD+"")
- getNetworkDate_noexit()
- `;
- return code;
- };
- // Blockly.Python.addFunction("wifiConnect", `def wifiConnect(text_ssid,text_password):
- // with open('/etc/wifi/wpa_supplicant.conf', 'w',encoding='utf-8') as f:
- // f.write((''.join([str(x) for x in ["ctrl_interface=/var/run/wpa_supplicant", "\\n", "update_config=1", "\\n\\n", "network={\\n ssid='"+text_ssid+"'\\n psk='"+text_password+"'\\n}"]]))+'\\r\\n')
- // getNetworkDate_noexit()
- // `);
- // var code = `
- // WiFiSSID = ${text_ssid}
- // WiFiPSD = ${text_password}
- // wifiConnect(WiFiSSID,WiFiPSD)
- // `;
- Blockly.Blocks['esp32_main_controller_wifi_disconnect'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_disconnect);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Disconnect_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_disconnect'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_os'] = 'import os\n'
- var code = 'os.system("wifi_disconnect_ap_test")';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_get_wifi_devices_number'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_hotspot_get_clients);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Devices_Number_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_wifi_devices_number'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'len(_WIFI.status("stations"))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_acquiring_equipment'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_hotspot_get_list);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Acquiring_Equipment_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_acquiring_equipment'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.status("stations")';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_get_wifi_connection_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_connection_status);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Connection_Status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_wifi_connection_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_os'] = `import os`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_http_client'] = `import http.client`;
- Blockly.Python.definitions_['wifi_is_content'] = `
- def wifi_is_content():
- global getDateNum
- cmd = "ping -c 1 123.58.32.151"
- res = os.popen(cmd).read()
- data = False
- if res.find("round-trip min/avg/max")>-1:
- data = True
-
- return data
- `
- // TODO: Change ORDER_NONE to the correct strength.
- let code = `wifi_is_content()`
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_close'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_close_hotspot);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Close_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_close'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.active(False)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_get_info'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_get_info)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_router_device_ip, "0"],
- // [Blockly.Msg.wifi_router_device_mask, "1"],
- // [Blockly.Msg.wifi_router_device_gateway, "2"],
- // [Blockly.Msg.wifi_router_device_dns, "3"]
- ]), "wifi_info");
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('wifi_info');
- var TOOLTIPS = {
- '0': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_ip),
- '1': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_mask),
- '2': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_gateway),
- '3': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_dns)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_get_info'] = function (block) {
- Blockly.Python.definitions_["v831_import_socket"] = `import socket`
- var dropdown_name = block.getFieldValue('wifi_info');
- Blockly.Python.addFunction("getWifiIP", `def getWifiIP():
- st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- try:
- #st.connect(('10.255.255.255', 1))
- st.connect(('8.8.8.8', 80))
- IP = st.getsockname()[0]
- except Exception:
- IP = '127.0.0.1'
- finally:
- st.close()
- return IP`, true)
- // TODO: Assemble Python into code variable.
- var code = 'getWifiIP()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_ntptime'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ntptime_header.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_ntptime_title);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Ntptime_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_ntptime'] = function (block) {
- // var dropdown_timezone = block.getFieldValue('timezone');
- // var text_website = block.getFieldValue('website');
- Blockly.Python.definitions_['v831_import_os'] = `import os`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_http_client'] = `import http.client`;
- Blockly.Python.definitions_['v831_wifi_getdate'] = `def getNetworkDate():
- try:
- coon = http.client.HTTPConnection("www.baidu.com")
- coon.request("GET","/")
- r = coon.getresponse()
- ts = r.getheader("date")
- GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
- BeiJing_time = time.localtime(time.mktime(GMT_time)+ 8*60*60)
- format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
- command = "date -s "+"\\"{}\\"".format(format_time)
- os.system(command)
- sys.exit()
- except:
- pass
- `
- // TODO: Assemble Python into code variable.
- var code = 'getNetworkDate()\n';
- return code;
- };
- // web
- // 获取天气
- Blockly.Blocks['v831_get_weather_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.v831_get_weather_init)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.v831_get_weather_init);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['v831_get_weather_init'] = function (block) {
- // var dropdown_timezone = block.getFieldValue('timezone');
- // var text_website = block.getFieldValue('website');
- Blockly.Python.definitions_['v831_import_request'] = `import requests`;
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['v831_set_city_and_time'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.set_weather_city)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.beijing, "101010100"],
- [Blockly.Msg.shanghai, "101020100"],
- [Blockly.Msg.guangzhou, "101280101"],
- [Blockly.Msg.shenzhen, "101280601"],
- [Blockly.Msg.HongKong, "101320101"],
- ]), "get_city")
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.set_weather_city_title);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['v831_set_city_and_time'] = function (block) {
- var city = block.getFieldValue('get_city');
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- Blockly.Python.definitions_['v831_import_bs4'] = `import bs4`;
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_get_weather_fuc'] = `def get_html(url):
- headers = {
- "User-Agent":
- "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
- "ContentType":
- "text/html; charset=utf-8",
- "Accept-Encoding":
- "gzip, deflate, sdch",
- "Accept-Language":
- "zh-CN,zh;q=0.8",
- "Connection":
- "keep-alive",
- }
- try:
- htmlcontet = requests.get(url, headers=headers, timeout=30)
- htmlcontet.raise_for_status()
- htmlcontet.encoding = "utf-8"
- return htmlcontet.text
- except:
- return " 请求失败 "
- def get_content(url):
- weather_list = []
- html = get_html(url)
- soup = bs4.BeautifulSoup(html, "lxml")
- content_ul = soup.find("div", class_="t").find("ul", class_="clearfix").find_all("li")
-
- for content in content_ul:
- try:
- weather = {}
- weather["D"] = content.find("h1").text
- weather["WEA"] = content.find("p", class_="wea").text
- weather["T"] = content.find(
- "p", class_="tem").span.text + content.find(
- "p", class_="tem").em.text
- weather_list.append(weather)
- except:
- print("查询不到")
- print(weather_list)
- return weather_list
- `
- var code = `get_content("http://www.weather.com.cn/weather1d/${city}.shtml")`;
- // if(title_small === "all"){
- // code = `getCityWeather_AllDay("${city}")['message']`
- // }else{
- // code = `getCityWeather_RealTime("${city}")['message']`
- // }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['v831_set_city_and_time_new'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.set_weather_city)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.beijing, "101010100"],
- [Blockly.Msg.shanghai, "101020100"],
- [Blockly.Msg.guangzhou, "101280101"],
- [Blockly.Msg.shenzhen, "101280601"],
- [Blockly.Msg.HongKong, "101320101"],
- ]), "get_city")
- // .appendField(Blockly.Msg.set_weather_title_small)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.set_weather_All_day_long, 'all'],[Blockly.Msg.set_weather_real_time, "real"]
- // ]), "title_small");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.set_weather_city_title);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['v831_set_city_and_time_new'] = function (block) {
- // var title_small = block.getFieldValue('title_small');
- var city = block.getFieldValue('get_city');
- Blockly.Python.addVariable("getWeatherResult", 'getWeatherResult = ""', true)
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_[""] = `def hefengGetWeather(city,lang):
- getWeatherUrl = f"https://devapi.qweather.com/v7/weather/now?location={city}&key=d8d1d01bac3246ee8c34640681c1c9b6&lang={lang}"
- weatherResult = ""
- try:
- responseResult = requests.get(getWeatherUrl)
- if responseResult.status_code == 200:
- weatherResult = responseResult.json()
- except:
- pass
- return weatherResult
- `
- var code = `getWeatherResult = hefengGetWeather(${city},"${localStorage.getItem("handPyLanguage")}")
- `;
- // if(title_small === "all"){
- // code = `getCityWeather_AllDay("${city}")['message']`
- // }else{
- // code = `getCityWeather_RealTime("${city}")['message']`
- // }
- return code;
- };
- Blockly.Blocks['v831_get_weather_result'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.weather_resulet, "result"],
- [Blockly.Msg.image_process_temperature, "temp"],
- [Blockly.Msg.weather_condition, "text"],
- [Blockly.Msg.windDir, "windDir"],
- [Blockly.Msg.windScale, "windScale"],
- [Blockly.Msg.windSpeed, "windSpeed"],
- [Blockly.Msg.image_process_humidity, "humidity"],
- ]), "get_weather")
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.set_weather_city_title);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['v831_get_weather_result'] = function (block) {
- var weather = block.getFieldValue('get_weather');
- var code = "";
- if (weather == "result") {
- code = `getWeatherResult`
- } else {
- code = `getWeatherResult["now"]["${weather}"]`
- }
- // if(title_small === "all"){
- // code = `getCityWeather_AllDay("${city}")['message']`
- // }else{
- // code = `getCityWeather_RealTime("${city}")['message']`
- // }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- IoT Service
- */
- Blockly.Blocks['esp32_main_controller_ThingSpeak_send_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 190, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_api_set)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_API_KEY"), "key");
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_thingspeak_send_data_1);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IOT_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ThingSpeak_send_data'] = function (block) {
- var text_key = block.getFieldValue('key');
- var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import requests\n' +
- 'import json\n'
- '';
- var code = '_THINGSPEAK_CHANNEL_API = "' + text_key + '"\n'
- Blockly.Python.addSetup("esp32_main_controller_ThingSpeak_send_data", code);
- var code = '_THINGSPEAK_DATA1 = ' + data + '\n';
- code += '_THINGSPEAK_ENDPOINT = "http://api.thingspeak.com/update?api_key=" + _THINGSPEAK_CHANNEL_API + "&field1=" + _THINGSPEAK_DATA1\n';
- code += '_THINGSPEAK_REQEUST = requests.get(_THINGSPEAK_ENDPOINT)\n';
- return code;
- };
- Blockly.Blocks['CocoRobo_set_max'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_set_max)
- .appendField(new Blockly.FieldTextInput("5"), "number");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#183895");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['CocoRobo_set_max'] = function (block) {
- var number = block.getFieldValue('number');
- Blockly.Python.definitions_['import_socket'] = 'import usocket as socket';
- var code = '_S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n';
- code += '_S.bind((_WIFI.ifconfig()[0], 80))\n';
- code += '_S.listen(' + number + ')\n';
- Blockly.Python.addSetup("CocoRobo_set_max", code);
- var code = '_SERVER_CONN, _SERVER_ADDR = _S.accept()\n';
- code += '_SERVER_REQUEST = _CONN.recv(1024)\n';
- return code;
- };
- Blockly.Blocks['CocoRobo_get_source'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_set_source);
- this.setOutput(true, null);
- this.setColour("#183895");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['CocoRobo_get_source'] = function (block) {
- var code = 'str(_SERVER_ADDR)\n';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['CocoRobo_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_response_http_content_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_response_text, "text"],
- [Blockly.Msg.internet_response_content, "content"],
- [Blockly.Msg.internet_response_status, "state"],
- [Blockly.Msg.internet_response_json, "json"],
- [Blockly.Msg.internet_response_encode, "code"],
- [Blockly.Msg.internet_response_reason, "reason"]
- ]), "op");
- this.setOutput(true, null);
- this.setColour("#183895");
- // this.setTooltip(Blockly.Msg.CocoRobo_get_TOOLTIP);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('op');
- var TOOLTIPS = {
- 'text': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_text),
- 'content': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_content),
- 'state': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_status),
- 'json': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_json),
- 'code': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_encode),
- 'reason': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_reason)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['CocoRobo_get'] = function (block) {
- var op = block.getFieldValue('op');
- var code = '_SEND_HTTP_REQUEST.text';
- if (op == "text") {
- code = '_SEND_HTTP_REQUEST.text';
- } else if (op == "content") {
- code = '_SEND_HTTP_REQUEST.content';
- } else if (op == "state") {
- code = '_SEND_HTTP_REQUEST.status_code';
- } else if (op == "json") {
- code = '_SEND_HTTP_REQUEST.json()';
- } else if (op == "code") {
- code = '_SEND_HTTP_REQUEST.encoding';
- } else if (op == "reason") {
- code = '_SEND_HTTP_REQUEST.reason';
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['iot_system_run'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_RUN);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.system_code_path);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setTooltip(Blockly.Msg.Iot_System_Run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_run = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _code = "exec(open(" + _path + ").read())\n";
- return _code;
- }
- Blockly.Blocks['iot_system_create_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_CREATE_FILE);
- this.appendValueInput("filename")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F_1);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setTooltip(Blockly.Msg.Iot_System_Create_File_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_create_file = function (block) {
- var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _code = `with open('${_path + _filename}', 'a') as f:
- f.write()
- `;
- return _code;
- }
- Blockly.Blocks['iot_system_open_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_OPEN_FILE);
- this.appendValueInput("filename")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F_1);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Iot_System_Open_File_TOOLTIP);
- }
- };
- Blockly.Python.iot_system_open_file = function (block) {
- var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _code = "f = open(\"" + _path + _filename + "\", 'a')\n";
- return _code;
- }
- Blockly.Blocks['iot_system_write_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_FILE);
- this.appendValueInput("WPATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_W);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('separator');
- var TOOLTIPS = {
- 'newline': Blockly.Msg.Iot_System_Write_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
- 'comma': Blockly.Msg.Iot_System_Write_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
- 'space': Blockly.Msg.Iot_System_Write_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
- 'semicolon': Blockly.Msg.Iot_System_Write_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python.iot_system_write_file = function (block) {
- var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
- var _separator = block.getFieldValue('separator');
- var _code = ""
- if (_separator == "newline") {
- _code = "f.write(" + _wpath + " + '\\r\\n')\n";
- } else if (_separator == "comma") {
- _code = "f.write(" + _wpath + " + ',')\n";
- } else if (_separator == "space") {
- _code = "f.write(" + _wpath + " + ' ')\n";
- } else if (_separator == "semicolon") {
- _code = "f.write(" + _wpath + " + ';')\n";
- }
- return _code;
- }
- Blockly.Blocks['iot_system_close_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_CLOSE_FILE);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.SYSTEM_CLOSE_FILE);
- }
- };
- Blockly.Python.iot_system_close_file = function (block) {
- var _code = "f.close()\n";
- return _code;
- }
- Blockly.Blocks['iot_system_input_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_INPUT_FILE);
- this.appendValueInput("filename")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setOutput(true, null);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- this.setColour(32);
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('separator');
- var TOOLTIPS = {
- 'newline': Blockly.Msg.Iot_System_Input_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
- 'comma': Blockly.Msg.Iot_System_Input_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
- 'space': Blockly.Msg.Iot_System_Input_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
- 'semicolon': Blockly.Msg.Iot_System_Input_File_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python.iot_system_input_file = function (block) {
- var _filename = Blockly.Python.valueToCode(block, 'filename', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC).replace("\"", "").replace("\"", "");
- var _separator = block.getFieldValue('separator');
- var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n"
- _code += " f = open(_path, 'r')\n"
- _code += " result = f.read().split(_sep)\n"
- _code += " f.close()\n"
- _code += " return result\n"
- Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
- _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", '\\r\\n')";
- if (_separator == "newline") {
- _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", '\\r\\n')";
- } else if (_separator == "comma") {
- _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ',')";
- } else if (_separator == "space") {
- _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ' ')";
- } else if (_separator == "semicolon") {
- _code = "_GET_LIST_FROM_FILE(\"" + _path + _filename + "\", ';')";
- }
- return [_code, Blockly.Python.ORDER_NONE];
- }
- Blockly.Blocks['iot_system_delete_file'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_system_delete);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F_1);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setTooltip(Blockly.Msg.iot_system_delete_file_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_delete_file = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_os'] = 'import os\n'
- var _code = "os.remove(" + _path + ")\n";
- return _code;
- }
- Blockly.Blocks['iot_system_restart'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Restart_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_restart = function (block) {
- Blockly.Python.definitions_.import_machine = "import machine";
- var _code = "machine.reset()\n"
- return _code;
- }
- Blockly.Blocks['iot_system_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_size);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_get = function (block) {
- var code = "_MCU_FREESPACE"
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- }
- Blockly.Blocks['iot_system_mac'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_mac);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Mac_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_mac = function (block) {
- Blockly.Python.definitions_.import_machine_ub = "import machine, ubinascii";
- var _code = "ubinascii.hexlify(machine.unique_id()).decode().upper()"
- return [_code, Blockly.Python.ORDER_CONDITIONAL];
- }
- Blockly.Blocks['iot_system_get_cocorobo_id'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_cocorobo_cid);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Get_Cocorobo_Id_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_system_get_cocorobo_id'] = function (block) {
- Blockly.Python.definitions_['import_cocorobo_cid'] = 'from cocorobo import cid\n';
- // TODO: Assemble Python into code variable.
- var code = 'cid._GET_MACHINE_ID().strip(\'\\n\')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_system_get_cocorobo_firmware_version'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_system_get_cocorobo_firmware_version);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.iot_system_get_cocorobo_firmware_version_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_system_get_cocorobo_firmware_version'] = function (block) {
- Blockly.Python.definitions_['import_cocorobo_cid'] = '' +
- 'try:\n' +
- ' from cocorobo import firmware_info\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' pass\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = 'firmware_info.iot()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_system_delete'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_system_delete);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.iot_system_delete_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_delete = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_os'] = 'import os\n'
- var _code = "os.remove(" + _path + ")\n";
- return _code;
- }
- /*
- _____ _____ __ _
- \_ \___/__ \ / _\ ___ _ ____ _(_) ___ ___
- / /\/ _ \ / /\/ \ \ / _ \ '__\ \ / / |/ __/ _ \
- /\/ /_| (_) / / _\ \ __/ | \ V /| | (_| __/
- \____/ \___/\/ \__/\___|_| \_/ |_|\___\___|
-
- */
- var ESP32_IOT_COLOR = "#3c7683";
- var IFTTT_BLOCK_COLOR = "#ff3b00";
- var ONENET_BLOCK_COLOR = "#00b5ff";
- Blockly.Blocks['esp32_main_controller_onenet_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 130, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_server)
- .appendField(new Blockly.FieldTextInput("183.230.40.39"), "server_ip");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_productid)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_ID"), "product_id");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_productkey)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_KEY"), "product_api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_deviceid)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_DEVICE_ID"), "device_id");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_setup'] = function (block) {
- var text_server_ip = block.getFieldValue('server_ip');
- var text_product_id = block.getFieldValue('product_id');
- var text_product_api_key = block.getFieldValue('product_api_key');
- var text_device_id = block.getFieldValue('device_id');
- // TODO: Assemble Python into code variable.
- var code = '...\n';
- return code;
- };
- Blockly.Blocks['iot_service_onenet'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_send);
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_onenet_create_with_item']));
- this.setColour(ONENET_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip("");
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_onenet_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_onenet_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_onenet_mqtt_send_item)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_onenet_create_with_container'] = {
- init: function () {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_onenet_create_with_item'] = {
- init: function () {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_onenet'] = function (block) {
- var code = "";
- // onenet_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['esp32_main_controller_onenet_when_receive_msg'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive);
- this.appendValueInput("content")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_when);
- this.appendStatementInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_exec);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_when_receive_msg'] = function (block) {
- var value_content = Blockly.Python.valueToCode(block, 'content', Blockly.Python.ORDER_ATOMIC);
- var statements_exec = Blockly.Python.statementToCode(block, 'exec');
- // TODO: Assemble Python into code variable.
- var code = '...\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_onenet_disconnect'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_disconnect);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_disconnect'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_ifttt_send_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_webhook)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "ifttt_api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_eventname)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "ifttt_event");
- this.appendValueInput("ifttt_data_1")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_1);
- this.appendValueInput("ifttt_data_2")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_2);
- this.appendValueInput("ifttt_data_3")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_3);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IFTTT_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Send_Data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ifttt_send_data'] = function (block) {
- var text_ifttt_api_key = block.getFieldValue('ifttt_api_key');
- var text_ifttt_event = block.getFieldValue('ifttt_event');
- var value_ifttt_data_1 = Blockly.Python.valueToCode(block, 'ifttt_data_1', Blockly.Python.ORDER_ATOMIC);
- var value_ifttt_data_2 = Blockly.Python.valueToCode(block, 'ifttt_data_2', Blockly.Python.ORDER_ATOMIC);
- var value_ifttt_data_3 = Blockly.Python.valueToCode(block, 'ifttt_data_3', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- var code = '' +
- '_IFTTT_POST_API_KEY = "' + text_ifttt_api_key + '"\n' +
- '_IFTTT_POST_EVENT_NAME = "' + text_ifttt_event + '"\n' +
- '_IFTTT_POST_ENDPOINT = "http://maker.ifttt.com/trigger/"+ _IFTTT_POST_EVENT_NAME + "/with/key/" + _IFTTT_POST_API_KEY\n' +
- '_IFTTT_POST_DATA = \'{"value1":"\'+ str(' + value_ifttt_data_1 + ') +\'","value2":"\'+ str(' + value_ifttt_data_2 + ') +\'","value3":"\'+ str(' + value_ifttt_data_3 + ') +\'"}\'\n' +
- '_IFTTT_POST_REQUEST = requests.post(_IFTTT_POST_ENDPOINT, data = _IFTTT_POST_DATA , headers = { "Content-type": "application/json" }, timeout=60)\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_ifttt_touched'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_webhook)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_eventname)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "timename");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IFTTT_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Touched_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ifttt_touched'] = function (block) {
- var text_ifttt_api_key = block.getFieldValue('key');
- var text_timename = block.getFieldValue('timename');
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- var code = '_IFTTT_TRIGGER_EVENT_NAME = "' + text_timename + '"\n'
- code += '_IFTTT_TRIGGER_API_KEY = "' + text_ifttt_api_key + '"\n'
- code += '_IFTTT_TRIGGER_ENDPOINT = "https://maker.ifttt.com/trigger/" + _IFTTT_TRIGGER_EVENT_NAME + "/with/key/" + _IFTTT_TRIGGER_API_KEY\n'
- Blockly.Python.addSetup("esp32_main_controller_ifttt_touched", code);
- var code = '_IFTTT_GET_REQUEST = requests.get(_IFTTT_TRIGGER_ENDPOINT, timeout=60)\n'
- return code;
- };
- IOT_THINGSPEAK_COLOR = "#1b379f";
- // IOT_THINGSPEAK_COLOR = ESP32_IOT_COLOR;
- Blockly.Blocks['iot_service_thingspeak'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_WRITE_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_thingspeak_create_with_item']));
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_thingspeak_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_thingspeak_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField("Field " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("field" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_thingspeak_create_with_container'] = {
- init: function () {
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_thingspeak_create_with_item'] = {
- init: function () {
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_thingspeak'] = function (block) {
- var first_input = Blockly.Python.valueToCode(block, 'first_input', Blockly.Python.ORDER_ATOMIC);
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_THINGSPEAK_REQUEST', '', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + api;
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = "field" + (n + 1);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
- Blockly.Python.ORDER_NONE) || '';
- thingspeak_url += '&' + item_field + '=\"+str(' + item_value + ')+\"';
- }
- var code = '' +
- '_THINGSPEAK_ENDPOINT = "' + thingspeak_url + '"\n' +
- '_THINGSPEAK_REQUEST = requests.get(_THINGSPEAK_ENDPOINT,timeout=60)\n' +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_thingspeak_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_query_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_query_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_READ_API_KEY"), "api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_channel_id)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_ID"), "id");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read'] = function (block) {
- var text_api_key = block.getFieldValue('api_key');
- var text_id = block.getFieldValue('id');
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- Blockly.Python.addVariable('_THINGSPEAK_READ_REQUEST', '', true);
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST = requests.get("https://api.thingspeak.com/channels/" + "' + text_id + '" + "/feeds.json?api_key=" + "' + text_api_key + '",timeout=60)\n';
- return code;
- };
- Blockly.Blocks['iot_service_thingspeak_read_total'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_read_total);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Total_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read_total'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST.json()[\'channel\'][\'last_entry_id\']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_thingspeak_read_specific'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_get_title);
- this.appendValueInput("entry")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeaK_get_field);
- this.appendValueInput("field")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeaK_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Specific_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read_specific'] = function (block) {
- var value_entry = Blockly.Python.valueToCode(block, 'entry', Blockly.Python.ORDER_ATOMIC);
- var value_field = Blockly.Python.valueToCode(block, 'field', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST.json()[\'feeds\'][' + value_entry + '][\'field' + value_field + '\']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- // KUJU
- var KUJU_BLOCK_COLOR = "#00b5ff";
- Blockly.Blocks['iot_service_KUJU_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/KUJU.png", 110, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_title);
- this.appendValueInput("username")
- .setCheck(null)
- .appendField(Blockly.Msg.KUJU_username);
- this.appendValueInput("password")
- .setCheck(null)
- .appendField(Blockly.Msg.KUJU_password);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_KUJU_setup'] = function (block) {
- var username = Blockly.Python.valueToCode(block, 'username', Blockly.Python.ORDER_ATOMIC);
- var password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '_COCOCLOUD_SEND_REQUEST = None', true);
- Blockly.Python.addVariable('token', 'token = ""', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- Blockly.Python.definitions_['import_iot_service_KUJU_get_sensor_data'] = '' +
- 'def GetSensorValue(typeCode):\n' +
- ' _COCOCLOUD_SEND_ENDPOINT = "http://ssl.kujuportal.com/apps/get_info"\n' +
- ' _COCOCLOUD_SEND_DATA = \'{"token":"\' + token + \'","type":"device_list"}\'\n' +
- ' try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA, headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- ' a = json.loads(str(_COCOCLOUD_SEND_REQUEST.content.decode("utf-8")).replace("\'","").replace("\'",""))\n' +
- ' if a["result"] == "1":\n' +
- ' info = a["info"]\n' +
- ' for i in range(len(info)):\n' +
- ' if info[i]["type_code"] == typeCode:\n' +
- ' _COCOCLOUD_SEND_ENDPOINT = "http://ssl.kujuportal.com/apps/get_info"\n' +
- ' _COCOCLOUD_SEND_DATA = \'{"token":"\' + token + \'","type":"device_status","gateway_sn":"\' + info[i]["gateway_sn"] + \'","system_id":"\' + info[i]["system_id"] + \'","type_code":"\' + info[i]["type_code"] + \'"}\'\n' +
- ' _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA, headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- ' a = json.loads(str(_COCOCLOUD_SEND_REQUEST.content.decode("utf-8")).replace("\'","").replace("\'",""))\n' +
- ' if a["result"] == "1":\n' +
- ' return a["info"]["value"]\n' +
- ' except BaseException as e:\n' +
- ' return 0\n' +
- '';
- var cococloud_data = "'{\"username\":" + username + ",\"password\":\"" + hex_md5(password.replace('"', '').replace('"', '')) + "\",\"type\":\"token\"}'";
- var code = '' +
- '_COCOCLOUD_SEND_ENDPOINT = "http://ssl.kujuportal.com/apps/user_token"\n' +
- '_COCOCLOUD_SEND_DATA = ' + cococloud_data + '\n' +
- 'try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- ' token = json.loads(str(_COCOCLOUD_SEND_REQUEST.content.decode("utf-8")).replace("\'","").replace("\'",""))["info"]["token"]\n' +
- // ' print(token)\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_KUJU_controll'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/KUJU.png", 110, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_controll_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_controll_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.KUJU_controll_action_0, "0"],
- [Blockly.Msg.KUJU_controll_action_1, "1"]
- ]), "type");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_controll_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_KUJU_controll'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var code = '' +
- '_COCOCLOUD_SEND_ENDPOINT = "http://ssl.kujuportal.com/apps/get_info"\n' +
- '_COCOCLOUD_SEND_DATA = \'{"token":"\' + token + \'","type":"device_list"}\'\n' +
- 'try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA, headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- ' a = json.loads(str(_COCOCLOUD_SEND_REQUEST.content.decode("utf-8")).replace("\'","").replace("\'",""))\n' +
- ' if a["result"] == "1":\n' +
- ' info = a["info"]\n' +
- ' for i in range(len(info)):\n' +
- ' if info[i]["type_code"] == "0010001":\n' +
- ' _COCOCLOUD_SEND_ENDPOINT = "http://ssl.kujuportal.com/apps/ctrl_device"\n' +
- ' _COCOCLOUD_SEND_DATA = \'{"token":"\' + token + \'","type":"device","gateway_sn":"\' + info[i]["gateway_sn"] + \'","system_id":"\' + info[i]["system_id"] + \'","type_code":"\' + info[i]["type_code"] + \'","value":' + dropdown_type + '}\'\n' +
- ' _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA, headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_KUJU_get_temperature_humidity_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/KUJU.png", 110, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_get_temperature_humidity_data_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_temperature, "temperature"],
- [Blockly.Msg.basic_humidity, "humidity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_get_temperature_humidity_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_KUJU_get_temperature_humidity_data'] = function (block) {
- var dropdown_type = block.getFieldValue('env_get_type');
- var code = '';
- if (dropdown_type == "temperature") {
- code = "GetSensorValue(\"0320101\")";
- }
- if (dropdown_type == "humidity") {
- code = "GetSensorValue(\"0320201\")";
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['iot_service_KUJU_get_motion_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/KUJU.png", 110, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_get_motion_data_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_motion_data, "move"],
- [Blockly.Msg.basic_illumination_intensity, "Illumination intensity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_get_motion_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_KUJU_get_motion_data'] = function (block) {
- var dropdown_type = block.getFieldValue('env_get_type');
- var code = '';
- if (dropdown_type == "move") {
- code = "GetSensorValue(\"0300101\")";
- }
- if (dropdown_type == "Illumination intensity") {
- code = "GetSensorValue(\"0300201\")";
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['iot_service_KUJU_get_sensor_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/KUJU.png", 110, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_get_sensor_data_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_water_sensor, "water sensor"],
- [Blockly.Msg.basic_door_sensor, "door sensor"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_get_sensor_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_KUJU_get_sensor_data'] = function (block) {
- var dropdown_type = block.getFieldValue('env_get_type');
- var code = '';
- if (dropdown_type == "water sensor") {
- code = "GetSensorValue(\"0120001\")";
- }
- if (dropdown_type == "door sensor") {
- code = "GetSensorValue(\"0080001\")";
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- // Wetech
- var WETECH_COLOR = "#3CB371";
- Blockly.Blocks['wifiServices_WeTech_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wetech_header.png", 150, 60, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifiServices_WeTech_title);
- this.appendValueInput("username")
- .setCheck(null)
- .appendField(Blockly.Msg.wifiServices_WeTech_username);
- this.appendValueInput("password")
- .setCheck(null)
- .appendField(Blockly.Msg.wifiServices_WeTech_password);
- this.appendValueInput("gateway_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY7)
- this.appendValueInput("mac_address")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY12)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_setup'] = function (block) {
- var username = Blockly.Python.valueToCode(block, 'username', Blockly.Python.ORDER_ATOMIC);
- var password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
- var gateway_id = Blockly.Python.valueToCode(block, 'gateway_id', Blockly.Python.ORDER_ATOMIC);
- var mac_address = Blockly.Python.valueToCode(block, 'mac_address', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_wetech_username', '_wetech_username = ' + username, true);
- Blockly.Python.addVariable('_wetech_password', '_wetech_password = ' + password, true);
- Blockly.Python.addVariable('_wetech_gateway_id', '_wetech_gateway_id = ' + gateway_id, true);
- Blockly.Python.addVariable('_wetech_mac_address', '_wetech_mac_address = ' + mac_address, true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'from umqtt_simple import MQTTClient\n' +
- 'import ubinascii\n' +
- 'import machine' +
- '';
- Blockly.Python.definitions_['import_wifiServices_WeTech_setup'] = '' +
- '\n' +
- '_wetech_mqtt_server = ""\n' +
- '_wetech_mqtt_username = ""\n' +
- '_wetech_mqtt_pw = ""\n' +
- '_WeTech_CLIENT_ID = ubinascii.hexlify(machine.unique_id())\n' +
- 'isMqttConnect = False\n' +
- 'MQTT_Data = None\n' +
- 'client = None\n' +
- '\n' +
- 'def restart_and_reconnect():\n' +
- ' print(\'Failed to connect to MQTT broker. Reconnecting...\')\n' +
- ' time.sleep(10)\n' +
- ' machine.reset()\n' +
- '\n' +
- 'def connect_and_subscribe(CLIENT_ID,server,TOPIC,username,password):\n' +
- ' c = MQTTClient(CLIENT_ID, server,1883,username,password,keepalive=60)\n' +
- ' c.set_callback(sub_cb) #set callback\n' +
- ' c.connect() #connect mqtt\n' +
- ' c.subscribe(TOPIC) #client subscribes to a topic\n' +
- ' time.sleep(0.1)\n' +
- ' return c\n' +
- '\n' +
- 'def sub_cb(topic, msg):\n' +
- ' global MQTT_Data\n' +
- ' MQTT_Data = json.loads(msg)[\'StatusList\']\n' +
- '\n' +
- 'def setup_mqtt(url, username, password):\n' +
- ' global _wetech_mqtt_server,_wetech_mqtt_username,_wetech_mqtt_pw\n' +
- ' if _wetech_mqtt_server == "" and _wetech_mqtt_username == "" and _wetech_mqtt_pw == "":\n' +
- ' _WeTech_SEND_DATA = \"language=en&pf=ios&username=\"+ username + \"&password=\" + password\n' +
- ' try:\n' +
- ' _WeTech_SEND_REQUEST = requests.post(url, data = _WeTech_SEND_DATA , headers = { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, timeout = 60)\n' +
- ' #print(str(_WeTech_SEND_REQUEST.status_code)+", "+str(_WeTech_SEND_REQUEST.content))\n' +
- ' _wetech_mqtt_server = _WeTech_SEND_REQUEST.json()[\'mqtt_url\']\n' +
- ' _wetech_mqtt_username = _WeTech_SEND_REQUEST.json()[\'mqtt_username\']\n' +
- ' _wetech_mqtt_pw = _WeTech_SEND_REQUEST.json()[\'mqtt_pw\']\n' +
- ' except BaseException as e:\n' +
- ' print(e)\n' +
- ' pass\n' +
- '\n' +
- 'def getStatusDeviceById(device_id):\n' +
- ' global _wetech_mqtt_server,_wetech_mqtt_username,_wetech_mqtt_pw,client, _wetech_mac_address, MQTT_Data,isMqttConnect\n' +
- ' data = \'{"SourceID":"\' + str(_WeTech_CLIENT_ID).replace("b\'","").replace("\'","") + \'", "DestID": "gateway/"\' + str(_wetech_mac_address) + \', "Cmd": "13", "DeviceID":"0"}\'\n' +
- ' setup_mqtt("http://ihouse.janwit.cn/ha/ios/process/checkLogin.php", _wetech_username, _wetech_password)\n' +
- ' if _wetech_mqtt_server != "" and _wetech_mqtt_username != "" and _wetech_mqtt_pw != "":\n' +
- ' if isMqttConnect == False:\n' +
- ' try:\n' +
- ' client = connect_and_subscribe(str(_WeTech_CLIENT_ID).replace("b\'","").replace("\'",""), _wetech_mqtt_server, "gateway/" + str(_wetech_mac_address) + "/" + str(_WeTech_CLIENT_ID).replace("b\'","").replace("\'",""), _wetech_mqtt_username, _wetech_mqtt_pw)\n' +
- ' time.sleep(0.1)\n' +
- ' isMqttConnect = True\n' +
- ' client.publish("gateway/" + str(_wetech_mac_address) , data, 1)\n' +
- ' time.sleep(0.1)\n' +
- ' client.wait_msg()\n' +
- ' return stringToHex(MQTT_Data,device_id)\n' +
- ' except OSError as e:\n' +
- ' restart_and_reconnect()\n' +
- ' else:\n' +
- ' try:\n' +
- ' client.publish("gateway/" + str(_wetech_mac_address) , data, 1)\n' +
- ' time.sleep(0.1)\n' +
- ' client.wait_msg()\n' +
- ' return stringToHex(MQTT_Data,device_id)\n' +
- ' except OSError as e:\n' +
- ' restart_and_reconnect()\n' +
- '\n' +
- 'def stringToHex(msg,device_id):\n' +
- ' status = None\n' +
- ' for item in msg:\n' +
- ' if str(item[\'id\']) == device_id:\n' +
- ' status = item[\'status\']\n' +
- ' break\n' +
- ' status_value =status[6:] + status[4:6] + status[2:4] + status[0:2]\n' +
- ' return round(float(int(status_value,16) / 100),2)\n' +
- '\n' +
- 'def decToHexstring(x):\n' +
- ' hex_str = \'{:04X}\'.format(data*100)\n' +
- ' data_h, data_l = hex_str[0:2], hex_str[2:4]\n' +
- ' return str(data_l) + str(data_h)\n' +
- '\n' +
- 'def controllDeviceById(url, username, password, gateway_id, device_id, status):\n' +
- ' _WeTech_SEND_DATA = \"language=en&pf=ios&username=\"+ username + \"&password=\" + password +\"&cmd=control_device_req&gateway_id="+ gateway_id +"&controlList=[{\\\"DeviceID\\\":\\\"" + device_id + "\\\",\\\"Status\\\":\\\""+ status +"\\\",\\\"delayTime\\\":\\\"0\\\"}]\"\n' +
- ' try:\n' +
- ' _WeTech_SEND_REQUEST = requests.post(url, data = _WeTech_SEND_DATA , headers = { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, timeout = 60)\n' +
- ' print(str(_WeTech_SEND_REQUEST.status_code)+", "+str(_WeTech_SEND_REQUEST.content))\n' +
- ' except BaseException as e:\n' +
- ' print(e)\n' +
- ' pass\n' +
- '\n' +
- '';
- var code = '';
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY6)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_action_0, "0"],
- [Blockly.Msg.WeTech_controll_action_1, "1"]
- ]), "type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- if (dropdown_type == 0) {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"FF000000\")\n";
- }
- else {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"00000000\")\n";
- }
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_second_way'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY8)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_action_0, "0"],
- [Blockly.Msg.WeTech_controll_action_1, "1"]
- ]), "type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_second_way_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_second_way'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- if (dropdown_type == 0) {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"FF000000\")\n";
- }
- else {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"00000000\")\n";
- }
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_curtain_sensor'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY9)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_action_0, "0"],
- [Blockly.Msg.WeTech_controll_action_1, "1"],
- [Blockly.Msg.WeTech_controll_action_2, "2"]
- ]), "type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_curtain_sensor_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_curtain_sensor'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = "";
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- if (dropdown_type == 0) {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"01000000\")\n";
- }
- else if (dropdown_type == 1) {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"00000000\")\n";
- }
- else {
- code += "" +
- "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"02000000\")\n";
- }
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_air_conditioning_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY10)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_air_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_mode_0, "00FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_1, "01FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_2, "03FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_3, "04FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_4, "05FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_5, "06FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_6, "07FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_7, "08FFFFFF"],
- [Blockly.Msg.WeTech_controll_mode_8, "09FFFFFF"],
- // [Blockly.Msg.WeTech_controll_mode_9, "F0FFFFFF"]
- ]), "type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_air_conditioning_mode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_air_conditioning_mode'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- var code = "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"" + dropdown_type + "\")\n";
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_wind_speed_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY10)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_wind_speed_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_action_0, "FF00FFFF"],
- [Blockly.Msg.WeTech_controll_mode_10, "FF01FFFF"],
- [Blockly.Msg.WeTech_controll_mode_11, "FF02FFFF"],
- [Blockly.Msg.WeTech_controll_mode_12, "FF03FFFF"],
- [Blockly.Msg.WeTech_controll_mode_13, "FF05FFFF"]
- ]), "type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_wind_speed_mode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_wind_speed_mode'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- var code = "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"" + dropdown_type + "\")\n";
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_temp'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY10)
- this.appendValueInput("ADD0")
- .setCheck(null)
- .appendField(Blockly.Msg.WeTech_controll_temp_content)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_temp_TOOLTIP);
- this.setHelpUrl("");
- },
- onchange: function () {
- var num = this.getChildren()[1].getFieldValue("NUM");
- if (num > 30) {
- this.getChildren()[1].getField("NUM").setValue("30");
- }
- else if (num < 16) {
- this.getChildren()[1].getField("NUM").setValue("16");
- }
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_temp'] = function (block) {
- var ADD0 = Blockly.Python.valueToCode(block, 'ADD0', Blockly.Python.ORDER_ATOMIC);
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- var code = "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"FFFF\" + decToHexstring(" + ADD0 + "))\n";
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_all_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY10)
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_air_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_mode_0, "00"],
- [Blockly.Msg.WeTech_controll_mode_1, "01"],
- [Blockly.Msg.WeTech_controll_mode_2, "03"],
- [Blockly.Msg.WeTech_controll_mode_3, "04"],
- [Blockly.Msg.WeTech_controll_mode_4, "05"],
- [Blockly.Msg.WeTech_controll_mode_5, "06"],
- [Blockly.Msg.WeTech_controll_mode_6, "07"],
- [Blockly.Msg.WeTech_controll_mode_7, "08"],
- [Blockly.Msg.WeTech_controll_mode_8, "09"],
- // [Blockly.Msg.WeTech_controll_mode_9, "9"]
- ]), "air_conditioning_mode_type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_wind_speed_content)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.WeTech_controll_action_0, "00"],
- [Blockly.Msg.WeTech_controll_mode_10, "01"],
- [Blockly.Msg.WeTech_controll_mode_11, "02"],
- [Blockly.Msg.WeTech_controll_mode_12, "03"],
- [Blockly.Msg.WeTech_controll_mode_13, "05"]
- ]), "speed_mode_type");
- this.appendValueInput("ADD0")
- .setCheck(null)
- .appendField(Blockly.Msg.WeTech_controll_temp_content)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_all_mode_TOOLTIP);
- this.setHelpUrl("");
- },
- onchange: function () {
- var num = this.getChildren()[1].getFieldValue("NUM");
- if (num > 30) {
- this.getChildren()[1].getField("NUM").setValue("30");
- }
- else if (num < 16) {
- this.getChildren()[1].getField("NUM").setValue("16");
- }
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_all_mode'] = function (block) {
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var air_conditioning_mode_type = block.getFieldValue('air_conditioning_mode_type');
- var speed_mode_type = block.getFieldValue('speed_mode_type');
- var ADD0 = Blockly.Python.valueToCode(block, 'ADD0', Blockly.Python.ORDER_ATOMIC);
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- var code = "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", \"" + air_conditioning_mode_type + speed_mode_type + "\" + decToHexstring(" + ADD0 + "))\n";
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_IR'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WIFIEASYMODE_WeTech_KEY11)
- this.appendValueInput("ADD0")
- .setCheck(null)
- .appendField(Blockly.Msg.WeTech_controll_IR_content)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_IR_TOOLTIP);
- this.setHelpUrl("");
- },
- onchange: function () {
- var num = this.getChildren()[1].getFieldValue("NUM");
- if (num > 255) {
- this.getChildren()[1].getField("NUM").setValue("255");
- }
- else if (num < 0) {
- this.getChildren()[1].getField("NUM").setValue("0");
- }
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_IR'] = function (block) {
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var ADD0 = Blockly.Python.valueToCode(block, 'ADD0', Blockly.Python.ORDER_ATOMIC);
- var item_url = "http://ihouse.janwit.cn/ha/ios/bsInterface.php";
- var code = "controllDeviceById(\"" + item_url + "\", _wetech_username, _wetech_password, _wetech_gateway_id, " + device_id + ", str('{:02X}'.format(" + ADD0 + ")) + \"000000\");\n";
- return code;
- };
- Blockly.Blocks['wifiServices_WeTech_controll_derail_get_power_meter'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_power_meter);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.WeTech_controll_power_meter_1)
- this.setOutput(true);
- this.setInputsInline(false);
- this.setColour(WETECH_COLOR);
- this.setTooltip(Blockly.Msg.wifiServices_WeTech_controll_derail_get_power_meter_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_get_power_meter'] = function (block) {
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = "getStatusDeviceById(" + device_id + ")";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['wifiServices_WeTech_controll_derail_get_sensor_number'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.WeTech_controll_power_meter);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.Wetech_controll_pm25_sensor, "0"],
- [Blockly.Msg.Wetech_controll_CO2_sensor, "1"],
- [Blockly.Msg.Wetech_controll_Temperature_sensor, "2"],
- [Blockly.Msg.Wetech_controll_Humidity_sensor, "3"]
- ]), "type")
- .appendField(Blockly.Msg.Wetech_controll_sensor_title);
- this.setOutput(true);
- this.setInputsInline(false);
- this.setColour(WETECH_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '0': Blockly.Msg.wifiServices_WeTech_controll_derail_get_sensor_number_TOOLTIP.replace('%1', Blockly.Msg.Wetech_controll_pm25_sensor),
- '1': Blockly.Msg.wifiServices_WeTech_controll_derail_get_sensor_number_TOOLTIP.replace('%1', Blockly.Msg.Wetech_controll_CO2_sensor),
- '2': Blockly.Msg.wifiServices_WeTech_controll_derail_get_sensor_number_TOOLTIP.replace('%1', Blockly.Msg.Wetech_controll_Temperature_sensor),
- '3': Blockly.Msg.wifiServices_WeTech_controll_derail_get_sensor_number_TOOLTIP.replace('%1', Blockly.Msg.Wetech_controll_Humidity_sensor)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['wifiServices_WeTech_controll_derail_get_sensor_number'] = function (block) {
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var type = block.getFieldValue('type');
- var code = "getStatusDeviceById(" + device_id + ")";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- // tuya
- var tuya_BLOCK_COLOR = "#FF5500";
- Blockly.Blocks['iot_service_tuya_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_title);
- this.appendValueInput("access_id")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_access_id);
- this.appendValueInput("access_secret")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_access_secret);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_tuya_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_setup'] = function (block) {
- var access_id = Blockly.Python.valueToCode(block, 'access_id', Blockly.Python.ORDER_ATOMIC);
- var access_secret = Blockly.Python.valueToCode(block, 'access_secret', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['import_iot_service_tuya_setup'] = '' +
- '\n' +
- 'import tuya\n' +
- 'import json\n' +
- '\n' +
- '\n' +
- 'access_id = ' + access_id + '\n' +
- 'access_secret = ' + access_secret + '\n' +
- '\n' +
- 'def getAccessToken():\n' +
- ' global access_id, access_secret\n' +
- ' return tuya.setToken(access_id,access_secret)\n' +
- '\n' +
- '\n' +
- 'def getTuyaDeviceList():\n' +
- ' deviceList = tuya.getData(\"/v1.0/iot-01/associated-users/devices\")\n' +
- ' dlist = []\n' +
- ' for i in range(len(deviceList["result"]["devices"])):\n' +
- ' dlist.append({\"id\":deviceList[\"result\"][\"devices\"][i][\"id\"],\"name\":deviceList[\"result\"][\"devices\"][i][\"name\"]})\n' +
- ' return dlist\n' +
- '\n' +
- '\n' +
- 'def getTuyaTempAndHumValue(url,sign):\n' +
- ' deviceInfo = tuya.getData(url)\n' +
- ' tuyaValue = 0.0\n' +
- ' for i in range(3):\n' +
- ' if sign == 0 and deviceInfo["result"][i]["code"] == \"va_temperature\":\n' +
- ' tuyaValue = deviceInfo["result"][i]["value"] / 100\n' +
- ' break\n' +
- ' if sign == 1 and deviceInfo["result"][i]["code"] == \"va_humidity\":\n' +
- ' tuyaValue = deviceInfo["result"][i]["value"] / 100\n' +
- ' break\n' +
- ' return tuyaValue\n' +
- '\n' +
- '\n' +
- 'def rgb2hsv(r, g, b):\n' +
- ' r, g, b = r/255.0, g/255.0, b/255.0\n' +
- ' mx = max(r, g, b)\n' +
- ' mn = min(r, g, b)\n' +
- ' m = mx-mn\n' +
- ' if mx == mn:\n' +
- ' h = 1\n' +
- ' elif mx == r:\n' +
- ' if g >= b:\n' +
- ' h = ((g-b)/m)*60\n' +
- ' else:\n' +
- ' h = ((g-b)/m)*60 + 360\n' +
- ' elif mx == g:\n' +
- ' h = ((b-r)/m)*60 + 120\n' +
- ' elif mx == b:\n' +
- ' h = ((r-g)/m)*60 + 240\n' +
- ' if mx == 0:\n' +
- ' s = 1\n' +
- ' else:\n' +
- ' s = m/mx\n' +
- ' v = mx\n' +
- ' H = h * 1.0\n' +
- ' S = s * 255.0\n' +
- ' V = v * 255.0\n' +
- ' return str({\"h\":H,\"s\":S,\"v\":V})\n' +
- '\n' +
- '';
- var code = '';
- return code;
- };
- Blockly.Blocks['iot_service_tuya_setToken'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_setToken_text);
- this.appendStatementInput("NAME")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_tuya_setToken_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_setToken'] = function (block) {
- var statements_name = Blockly.Python.statementToCode(block, 'NAME');
- var code = '' +
- 'if (getAccessToken() == True):\n' +
- statements_name +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_tuya_getDeviceList'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_getDeviceList_text);
- this.setOutput(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_tuya_setToken_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_getDeviceList'] = function (block) {
- var code = 'getTuyaDeviceList()';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_tuya_controll_light_belt'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_device_id);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_des)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_0, "0"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_1, "1"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_2, "2"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_3, "3"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_4, "4"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_5, "5"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_6, "6"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_7, "7"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_8, "8"],
- [Blockly.Msg.iot_service_tuya_controll_light_belt_status_9, "9"]
- ]), "status");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_tuya_controll_light_belt_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_controll_light_belt'] = function (block) {
- var dropdown_type = block.getFieldValue('status');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (dropdown_type == "0") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"switch_led\",\"value\": false}]}')\n";
- }
- else if (dropdown_type == "1") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"switch_led\",\"value\": true}]}')\n";
- }
- else if (dropdown_type == "2") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"flash_scene_1\",\"value\": {\"bright\":255,\"frequency\":80,\"hsv\":[{\"h\":120.0,\"s\":255.0,\"v\":255.0}],\"temperature\":255}}]}')\n";
- }
- else if (dropdown_type == "3") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"flash_scene_2\",\"value\": {\"bright\":255,\"frequency\":190,\"hsv\":[{\"h\":0.0,\"s\":255.0,\"v\":255.0},{\"h\":120.0,\"s\":255.0,\"v\":255.0},{\"h\":240.0,\"s\":255.0,\"v\":255.0},{\"h\":54.8,\"s\":255.0,\"v\":255.0},{\"h\":183.1,\"s\":255.0,\"v\":255.0},{\"h\":288.0,\"s\":255.0,\"v\":255.0}],\"temperature\":255}}]}')\n";
- }
- else if (dropdown_type == "4") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"flash_scene_3\",\"value\": {\"bright\":255,\"frequency\":80,\"hsv\":[{\"h\":0.0,\"s\":255.0,\"v\":255.0}],\"temperature\":255}}]}')\n";
- }
- else if (dropdown_type == "5") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"flash_scene_4\",\"value\": {\"bright\":255,\"frequency\":55,\"hsv\":[{\"h\":0.0,\"s\":255.0,\"v\":255.0},{\"h\":120.0,\"s\":255.0,\"v\":255.0},{\"h\":60.0,\"s\":255.0,\"v\":255.0},{\"h\":300.0,\"s\":255.0,\"v\":255.0},{\"h\":240.0,\"s\":255.0,\"v\":255.0},{\"h\":0.0,\"s\":255.0,\"v\":255.0}],\"temperature\":255}}]}')\n";
- }
- else if (dropdown_type == "6") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"scene_data\",\"value\": {\"h\":37.5,\"s\":255.0,\"v\":189.0}}]}')\n";
- }
- else if (dropdown_type == "7") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"scene_data\",\"value\": {\"h\":37.5,\"s\":8.0,\"v\":255.0}}]}')\n";
- }
- else if (dropdown_type == "8") {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"scene_data\",\"value\": {\"h\":16.2,\"s\":255.0,\"v\":207.0}}]}')\n";
- }
- else {
- code += "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"scene_data\",\"value\": {\"h\":226.0,\"s\":175.7,\"v\":180.0}}]}')\n";
- }
- return code;
- };
- Blockly.Blocks['iot_service_tuya_controll_light_belt_color_value'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_color_value_title);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_device_id);
- this.appendValueInput("COLOR")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_color_value_title1);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_tuya_controll_light_belt_color_value_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_controll_light_belt_color_value'] = function (block) {
- var COLOR = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC).split("(")[2].split(")")[0];
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = "tuya.postData(\"/v1.0/devices/\"+" + device_id + "+\"/commands\",'{\"commands\": [{\"code\": \"work_mode\",\"value\": \"colour\"},{\"code\": \"colour_data\",\"value\": '+ rgb2hsv(" + COLOR + ") + '}]}')\n";
- // TODO: Change ORDER_NONE to the correct strength.
- return code;
- };
- Blockly.Blocks['iot_service_tuya_get_temperature_humidity_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.KUJU_get_temperature_humidity_data_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_temperature, "temperature"],
- [Blockly.Msg.basic_humidity, "humidity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.iot_service_tuya_controll_light_belt_device_id);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(tuya_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_KUJU_get_temperature_humidity_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_tuya_get_temperature_humidity_data'] = function (block) {
- var dropdown_type = block.getFieldValue('env_get_type');
- var device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (dropdown_type == "temperature") {
- code = "getTuyaTempAndHumValue(\"/v1.0/devices/\"+" + device_id + "+\"/status\",0)";
- }
- if (dropdown_type == "humidity") {
- code = "getTuyaTempAndHumValue(\"/v1.0/devices/\"+" + device_id + "+\"/status\",1)";
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- // cococloud
- var COCOCLOUD_BLOCK_COLOR = "#4085de";
- Blockly.Blocks['iot_service_cococloud'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_cococloud_create_with_item']));
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_cococloud_create_with_container'] = {
- init: function () {
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_cococloud_create_with_item'] = {
- init: function () {
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_cococloud'] = function (block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '_COCOCLOUD_SEND_REQUEST = None', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var cococloud_data = cococloud_data = '{';
- for (var n = 0; n < this.itemCount_; n++) {
- console.log(block.getFieldValue("field" + n))
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- cococloud_data += `"${item_field}":${item_value},`
- // cococloud_data += "+ item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var cococloud_data_final = cococloud_data.slice(0, -1) + '}';
- var code = `
- _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/${api}"
- _COCOCLOUD_SEND_DATA = ${cococloud_data_final}
- try:
- _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
- print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
- except BaseException as e:
- print(e)
- pass
- `
- return code;
- };
- Blockly.Blocks['v831_service_cococloud'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_cococloud_create_with_item']));
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('KEY' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'KEY' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('KEY' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('KEY' + i) && !this.getInput('ADD' + i)) {
- var input = this.appendValueInput('KEY' + i);
- input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
- // .appendField(new Blockly.FieldValueInput("KEY" + i));
- var ADD = this.appendValueInput("ADD" + i)
- ADD.appendField(Blockly.Msg.iotservice_cococloud_send_property_1)
- }
- }
- while (this.getInput('KEY' + i) && this.getInput('ADD' + i)) {
- this.removeInput('KEY' + i);
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Python['v831_service_cococloud'] = function (block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '_COCOCLOUD_SEND_REQUEST = None', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var cococloud_data = cococloud_data = '{';
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = Blockly.Python.valueToCode(this, 'KEY' + n, Blockly.Python.ORDER_NONE) || '';
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- cococloud_data += `${item_field}:${item_value},`
- // cococloud_data += "+ item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var cococloud_data_final = cococloud_data.slice(0, -1) + '}';
- console.log(cococloud_data_final)
- var code = `
- _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/${api}"
- _COCOCLOUD_SEND_DATA = ${cococloud_data_final}
- try:
- _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
- print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
- except BaseException as e:
- print(e)
- pass
- `;
- return code;
- };
- Blockly.Blocks['iot_service_cococloud_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "api");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_cococloud_read'] = function (block) {
- var api = block.getFieldValue('api');
- Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '_COCOCLOUD_READ_REQUEST = None', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST = requests.get("http://api.cocorobo.cn/iot/data/eventAPIKeyJson/" + "' + api + '", timeout=60)\n';
- return code;
- };
- Blockly.Blocks['iot_service_cococloud_read_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_property);
- this.appendValueInput("property")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_data);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_Data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_cococloud_read_data'] = function (block) {
- var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- __ _ _ __ _
- /\ \ \___| |___ _____ _ __| | __ /__\ ___ __ _ _ _ ___ ___| |_
- / \/ / _ \ __\ \ /\ / / _ \| '__| |/ / / \/// _ \/ _` | | | |/ _ \/ __| __|
- / /\ / __/ |_ \ V V / (_) | | | < / _ \ __/ (_| | |_| | __/\__ \ |_
- \_\ \/ \___|\__| \_/\_/ \___/|_| |_|\_\ \/ \_/\___|\__, |\__,_|\___||___/\__|
- |_|
- */
- var NETWORK_BLOCK_COLOR = "#16318a";
- Blockly.Blocks['esp32_network_http_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_header_get.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_title);
- this.appendValueInput("http_get_url")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_get_url);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_get'] = function (block) {
- var url = Blockly.Python.valueToCode(block, 'http_get_url', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_SEND_HTTP_GET_ENDPOINT', '', true);
- Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- Blockly.Python.definitions_['v831_import_SEND_HTTP'] = `_SEND_HTTP_GET_ENDPOINT = ${url}`;
- // TODO: Assemble Python into code variable.
- var code = '_SEND_HTTP_REQUEST = requests.get(_SEND_HTTP_GET_ENDPOINT)\n';
- return code;
- };
- Blockly.Blocks['cocopi_email_send'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.send_email_title);
- this.appendValueInput("email")
- .setCheck(null)
- .appendField(Blockly.Msg.send_email_to);
- this.appendValueInput("email_title")
- .setCheck(null)
- .appendField(Blockly.Msg.send_email_content_title);
- this.appendValueInput("email_content")
- .setCheck(null)
- .appendField(Blockly.Msg.send_email_content);
- // this.setOutput(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['cocopi_email_send'] = function (block) {
- var Email = Blockly.Python.valueToCode(block, 'email', Blockly.Python.ORDER_ATOMIC);
- var EmailTitle = Blockly.Python.valueToCode(block, 'email_title', Blockly.Python.ORDER_ATOMIC);
- var EmailContent = Blockly.Python.valueToCode(block, 'email_content', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_requests'] = `import requests`
- Blockly.Python.definitions_['v831_import_json'] = `import json`;
- // TODO: Assemble Python into code variable.
- Blockly.Python.addFunction(`send_email`, `def send_email(email,content,title):
- params = {
- "email": email,
- "content": content,
- "subject":title,
- "content_type":'plain'
- }
- emailURL = "https://traininguser-api.cocorobo.cn/sendEmail"
- try:
- r = requests.post(emailURL,json=params)
- print(json.loads(r.text))
- except Exception as e:
- print(e)
- `)
- var code = `send_email(${Email},${EmailContent},${EmailTitle})`;
- return code;
- };
- Blockly.Blocks['esp32_network_http_post'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_header_post.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_post_title);
- this.appendValueInput("http_post_url")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_post_url);
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_post_json);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Post_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_post'] = function (block) {
- var url = Blockly.Python.valueToCode(block, 'http_post_url', Blockly.Python.ORDER_ATOMIC);
- var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_SEND_HTTP_POST_ENDPOINT', '', true);
- Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
- Blockly.Python.definitions_['831_import_requests'] = `import requests`
- Blockly.Python.definitions_['831_import_json'] = `import json`
- Blockly.Python.definitions_['import_urequests'] = '' +
- '_SEND_HTTP_POST_ENDPOINT = ' + url + '\n' +
- '';
- // TODO: Assemble Python into code variable.
- // var code = '' +
- // '_SEND_HTTP_POST_DATA = \'' + data + '\'\n' +
- // '_SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, data = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })\n' +
- // '';
- var code = `_SEND_HTTP_POST_DATA = ${data}
- _SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, json = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })
- `
- return code;
- };
- Blockly.Blocks['esp32_network_http_get_data_from_local_server'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_server_data_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_server_data_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_http_get_server_data_json, "json"],
- [Blockly.Msg.internet_http_get_server_data_text, "text"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'json': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_json),
- 'text': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_text)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_get_data_from_local_server'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "text") {
- var request_code = "_SEND_HTTP_REQUEST.text";
- } else if (dropdown_type == "json") {
- var request_code = "eval(_SEND_HTTP_REQUEST.text)";
- }
- var code = request_code;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_network_http_server_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Server_Setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_setup'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['esp32_network_http_server_setup_def_setup'] = `from http.server import HTTPServer, SimpleHTTPRequestHandler`;
- let allSetHttpBlock = getBlocksByTypeName("esp32_network_http_server_route")
- let listt = allSetHttpBlock[0].innerText.split('/')
- console.log(listt)
- for (let i = 0; i < listt.length; i++) {
- console.log(listt[i].indexOf("get"),i)
- // if (listt[i].indexOf("get") != -1) {
- // console.log(listt[i].splice(0, listt[i].indexOf("get")))
- // }else if(listt[i].index("post") != -1){
- // console.log(listt[i].splice(0, listt[i].indexOf("post")))
- // }
- }
-
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_route'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_path)
- .appendField(new Blockly.FieldTextInput("/"), "path")
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_being_requested);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_property)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_route_setting_get, "get"],
- [Blockly.Msg.wifi_web_http_server_route_setting_post, "post"]
- ]), "response_type");
- this.appendStatementInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_do);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('response_type');
- var TOOLTIPS = {
- 'get': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_get),
- 'post': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_post)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_network_http_server_route'] = function (block) {
- var dropdown_response_type = block.getFieldValue('response_type');
- var text_path = block.getFieldValue('path');
- var statements_exec = Blockly.Python.statementToCode(block, 'exec');
- var response_type = "";
- var response_writeok = "";
- if (dropdown_response_type == "get") {
- response_type = "";
- } else if (dropdown_response_type == "post") {
- response_type = ", 'POST'";
- }
- var code = `server_address = ('', 8008)
- httpd = HTTPServer(server_address, MyHandler)
- httpd.serve_forever()\n`;
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_route_respond'] = {
- init: function () {
- this.appendValueInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_web_http_server_respond_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_respond_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_respond_text, "text/plain"],
- [Blockly.Msg.wifi_web_http_server_respond_json, "application/json"],
- [Blockly.Msg.wifi_web_http_server_respond_html, "text/html"]
- ]), "response_type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('response_type');
- var TOOLTIPS = {
- 'text/plain': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_text),
- 'application/json': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_json),
- 'text/html': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_html)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_route_respond'] = function (block) {
- var dropdown_response_type = block.getFieldValue('response_type');
- var statements_exec = Blockly.Python.valueToCode(block, 'exec', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'content = ' + statements_exec + '\n' +
- 'httpResponse.WriteResponseOk( headers = None,\n' +
- ' contentType = "' + dropdown_response_type + '",\n' +
- ' contentCharset = "UTF-8",\n' +
- ' content = str(content) )\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_get_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_get_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_get_as_json, "json"],
- [Blockly.Msg.wifi_web_http_server_get_as_text, "text"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'json': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_json),
- 'text': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_text)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_get_data'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- if (dropdown_type == "json") {
- var as_type = "ReadRequestContentAsJSON()";
- } else if (dropdown_type == "text") {
- var as_type = "ReadRequestContent(size=None)";
- }
- // TODO: Assemble Python into code variable.
- var code = 'httpClient.' + as_type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- var WS_BLOCK_COLOR = "#83178a";
- Blockly.Blocks['esp32_web_ws_setup_send'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ws_header_send.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_send_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_send_ip)
- .appendField(new Blockly.FieldTextInput("192.168.4.1"), "ip");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Send_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_setup_send'] = function (block) {
- var text_ip = block.getFieldValue('ip');
- Blockly.Python.definitions_['ws_setup_send'] = '' +
- 'import uwebsockets.client\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_client_websocket = uwebsockets.client.connect("ws://' + text_ip + ':80")\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_send'] = {
- init: function () {
- this.appendValueInput("command")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_ws_send_msg);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Send_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_send'] = function (block) {
- var value_command = Blockly.Python.valueToCode(block, 'command', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'message = ' + value_command + '\n' +
- '_client_websocket.send(message)\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_setup_receive'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ws_header_receive.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_recv);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Receive_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_setup_receive'] = function (block) {
- Blockly.Python.definitions_['ws_setup_receive'] = '' +
- 'import uasyncio, logging\n' +
- 'from uasyncio.websocket.server import WSReader, WSWriter\n' +
- 'logging.basicConfig(level=logging.DEBUG)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_receive_statement'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_receiving);
- this.appendStatementInput("command")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_ws_setup_recv_exec);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Receive_Statement_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_receive_statement'] = function (block) {
- var statements_command = Blockly.Python.statementToCode(block, 'command');
- Blockly.Python.definitions_['ws_setup_receive_statement'] = '' +
- 'def _ws_exec(msg):\n' +
- '' + statements_command +
- '\n' +
- 'def _ws_echo(reader, writer):\n' +
- ' yield from reader.readline()\n' +
- '\n' +
- ' reader = yield from WSReader(reader, writer)\n' +
- ' writer = WSWriter(reader, writer)\n' +
- '\n' +
- ' while 1:\n' +
- ' _ws_l = yield from reader.read(1024)\n' +
- ' print(_ws_l)\n' +
- ' _ws_exec(_ws_l)\n' +
- '\n' +
- ' if _ws_l == b"\\r":\n' +
- ' await writer.awrite(b"\\r\\n")\n' +
- ' else:\n' +
- ' await writer.awrite(_ws_l)\n' +
- '\n' +
- '_ws_loop = uasyncio.get_event_loop()\n' +
- '_ws_loop.create_task(uasyncio.start_server(_ws_echo, "0.0.0.0", 80))\n' +
- '_ws_loop.run_forever()\n' +
- '_ws_loop.close()\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_receive_get_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_get_msg);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_get_msg_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_ws_setup_get_msg_text, "text"],
- [Blockly.Msg.internet_ws_setup_get_msg_json, "json"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(WS_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'text': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_text),
- 'json': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_json)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_receive_get_data'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "text") {
- var code = "msg.decode(\"utf-8\")";
- } else if (dropdown_type == "json") {
- var code = "eval(msg.decode(\"utf-8\"))";
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- ____ _ _____
- | __ )| | | ____|
- | _ \| | | _|
- | |_) | |___| |___
- |____/|_____|_____|
- */
- var BLE_BLOCK_COLOR = "#629978";
- Blockly.Blocks['esp32_ble_setup_sender'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_sender_msg + " " + Blockly.Msg.esp32_ble_setup_sender);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_set_uuid)
- .appendField(new Blockly.FieldTextInput("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"), "uuid");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_setup_sender_TOOLTIP);
- this.setHelpUrl("https://www.uuidgenerator.net/version1");
- }
- };
- Blockly.Python['esp32_ble_setup_sender'] = function (block) {
- var text_uuid = block.getFieldValue('uuid');
- Blockly.Python.definitions_['esp32_ble_setup_sender_def'] = '' +
- '# Generate UUID via: https://www.uuidgenerator.net/version1\n' +
- 'import ble_simple_central, bluetooth, time, machine\n' +
- 'from ble_simple_central import BLESimpleCentral\n' +
- '\n' +
- 'ble, ble_read, not_found = bluetooth.BLE(), \'\', False\n' +
- 'central = BLESimpleCentral(ble, uuid = "' + text_uuid + '")\n' +
- '\n' +
- 'def ble_on_rx(v):\n' +
- ' global ble_read\n' +
- ' ble_read = v.decode("utf-8")\n' +
- '\n' +
- 'def ble_on_scan(addr_type, addr, name):\n' +
- ' if addr_type is not None:\n' +
- ' print("Found peripheral:", addr_type, addr, name)\n' +
- ' central.connect()\n' +
- ' else:\n' +
- ' global not_found\n' +
- ' not_found = True\n' +
- ' print("\\nNo peripheral found.")\n' +
- '\n' +
- 'def ble_scan_and_connect():\n' +
- ' central.scan(callback=ble_on_scan)\n' +
- ' print("Connecting...",end="")\n' +
- ' failed = 0\n' +
- ' while not central.is_connected():\n' +
- ' time.sleep_ms(100)\n' +
- ' if not_found: return\n' +
- ' print("Connected")\n' +
- '\n' +
- 'central.on_notify(ble_on_rx)\n' +
- 'with_response = False\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'try: ble_scan_and_connect()\n' +
- 'except BaseException as e: print(e)\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['esp32_ble_send_data'] = {
- init: function () {
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.esp32_ble_send_data_msg);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_send_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_send_data'] = function (block) {
- var value_data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- let allBlocks = block.workspace.getAllBlocks();
- let isReceiver = allBlocks.findIndex(item => {
- return item.type == "esp32_ble_setup_receiver";
- });
- let isReceiverAndDisabled = allBlocks.findIndex(item => {
- return item.type == "esp32_ble_setup_receiver" && item.disabled == true;
- });
- // TODO: Assemble Python into code variable.
- if (isReceiver == -1 || (isReceiver != -1 && isReceiverAndDisabled != -1)) {
- code = 'central_write_value = str(' + value_data + ')\n' +
- 'central.write(central_write_value, with_response)\n' +
- 'time.sleep_ms(400 if with_response else 30)\n' +
- '';
- }
- else if (isReceiver != -1 && isReceiverAndDisabled == -1) {
- code = 'central_send_value = str(' + value_data + ')\n' +
- 'ble_p.send(central_send_value)\n' +
- 'time.sleep_ms(400)\n' +
- '';
- }
- return code;
- };
- Blockly.Blocks['esp32_ble_setup_receiver'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_receiver_msg + " " + Blockly.Msg.esp32_ble_setup_receiver);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_set_uuid)
- .appendField(new Blockly.FieldTextInput("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"), "uuid");
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_set_name)
- .appendField(new Blockly.FieldTextInput("coco-ble"), "name")
- .appendField(Blockly.Msg.esp32_ble_set_name_limit);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_setup_receiver_TOOLTIP);
- this.setHelpUrl("https://www.uuidgenerator.net/version1");
- }
- };
- Blockly.Python['esp32_ble_setup_receiver'] = function (block) {
- var text_uuid = block.getFieldValue('uuid');
- var text_name = block.getFieldValue('name');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['esp32_ble_setup_receiver_def'] = '' +
- 'import ble_simple_peripheral, machine\n' +
- 'from ble_simple_peripheral import BLESimplePeripheral\n' +
- 'import bluetooth, time\n' +
- '\n' +
- 'ble = bluetooth.BLE()\n' +
- 'ble_p = BLESimplePeripheral(ble,uuid="' + text_uuid + '",name="' + text_name + '")\n' +
- 'ble_read = \'\'\n' +
- '\n' +
- 'def ble_on_rx(v):\n' +
- ' global ble_read\n' +
- ' ble_read = v.decode("utf-8")\n' +
- '\n' +
- '';
- var code = 'ble_p.on_write(ble_on_rx)\n';
- return code;
- };
- Blockly.Blocks['esp32_ble_get_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_get_data_msg);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_get_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_get_data'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'ble_read';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_ble_sender_connected'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_sender_connected_msg);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_sender_connected_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_sender_connected'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'central.is_connected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_ble_receiver_connected'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_receiver_connected_msg);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_receiver_connected_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_receiver_connected'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'ble_p.is_connected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- var EXPAND_COLOR = "#3163ed";
- Blockly.Blocks['esp32_expand_io_from_digital_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_expand_io_from_digital_pin)
- .appendField(new Blockly.FieldDropdown([
- ["4", "4"],
- ["12", "12"],
- ["13", "13"],
- ["14", "14"],
- ["15", "15"],
- ["16", "16"],
- ["17", "17"],
- ["21", "21"],
- ["22", "22"],
- ["25", "25"],
- ["27", "27"]
- ]), "type")
- .appendField(Blockly.Msg.digital_get_value);
- this.setOutput(true, null);
- this.setColour(EXPAND_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '4': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "4"),
- '12': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "12"),
- '13': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "13"),
- '14': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "14"),
- '15': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "15"),
- '16': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "16"),
- '17': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "17"),
- '21': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "21"),
- '22': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "22"),
- '25': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "25"),
- '27': Blockly.Msg.esp32_expand_io_from_digital_pin_TOOLTIP.replace('%1', "27")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_expand_io_from_digital_pin'] = function (block) {
- var type = block.getFieldValue('type');
- Blockly.Python.definitions_['import_esp32_expand_io_from_digital_pin_setup'] = '' +
- 'from machine import Pin\n' +
- '';
- let aa = 'p' + type + ' = Pin(' + type + ', Pin.IN)\n';
- Blockly.Python.addVariable('p' + type, aa, true);
- var code = 'p' + type + '.value()'
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_expand_io_set_digital_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_expand_io_set_digital_pin)
- .appendField(new Blockly.FieldDropdown([
- ["4", "4"],
- ["12", "12"],
- ["13", "13"],
- ["14", "14"],
- ["15", "15"],
- ["16", "16"],
- ["17", "17"],
- ["21", "21"],
- ["22", "22"],
- ["25", "25"],
- ["27", "27"]
- ]), "type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_set_as);
- this.appendValueInput("high_low")
- .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(EXPAND_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '4': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "4"),
- '12': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "12"),
- '13': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "13"),
- '14': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "14"),
- '15': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "15"),
- '16': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "16"),
- '17': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "17"),
- '21': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "21"),
- '22': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "22"),
- '25': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "25"),
- '27': Blockly.Msg.esp32_expand_io_set_digital_pin_TOOLTIP.replace('%1', "27")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_expand_io_set_digital_pin'] = function (block) {
- var type = block.getFieldValue('type');
- var value_name2 = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['import_esp32_expand_io_set_digital_pin_setup'] = '' +
- 'from machine import Pin\n' +
- '';
- let aa = '_PIN_OUT_' + type + ' = Pin(' + type + ', Pin.OUT)\n';
- Blockly.Python.addVariable('_PIN_OUT_' + type, aa, true);
- var code = '_PIN_OUT_' + type + '.value' + value_name2 + '\n';
- // TODO: Change ORDER_NONE to the correct strength.
- return code;
- };
- Blockly.Blocks['esp32_expand_io_set_analog_pin'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_expand_io_set_analog_pin)
- .appendField(new Blockly.FieldDropdown([
- ["4", "4"],
- ["12", "12"],
- ["13", "13"],
- ["14", "14"],
- ["15", "15"],
- ["16", "16"],
- ["17", "17"],
- ["21", "21"],
- ["22", "22"],
- ["25", "25"],
- ["27", "27"]
- ]), "type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_expand_io_set_analog_pin_digital_set_as);
- this.appendValueInput("digital_pin_input")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_expand_io_set_analog_pin_digital_set_af);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(EXPAND_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '4': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "4"),
- '12': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "12"),
- '13': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "13"),
- '14': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "14"),
- '15': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "15"),
- '16': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "16"),
- '17': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "17"),
- '21': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "21"),
- '22': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "22"),
- '25': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "25"),
- '27': Blockly.Msg.esp32_expand_io_set_analog_pin_TOOLTIP.replace('%1', "27")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_expand_io_set_analog_pin'] = function (block) {
- var type = block.getFieldValue('type');
- var value_name1 = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('pwm' + type, '', true);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_esp32_expand_io_set_analog_pin_setup'] = '' +
- 'from machine import Pin, PWM\n' +
- '';
- var code = 'pwm' + type + ' = PWM(Pin(' + type + '), freq = 80000, duty = 4 * ' + value_name1 + ')\n';
- return code;
- };
- /**
- * Turtles!
- */
- var Turtle_HUE = "#5cb2d6";
- Blockly.Blocks['turtle_create'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.TURTLE_CREATE_NE_TURTLE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setOutput(false);
- this.setInputsInline(true);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_CREATE_NE_TURTLE_TLTLE_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_create'] = function (block) {
- Blockly.Python.definitions_['import_turtle_setup'] = '' +
- 'import turtle\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = 'turtle.initTurtle()\n';
- return code;
- };
- Blockly.Blocks['turtle_color_set'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.image_process_set_filled_screen_color_black, "black"],
- [Blockly.Msg.image_process_set_filled_screen_color_red, "red"],
- [Blockly.Msg.image_process_set_filled_screen_color_blue, "blue"],
- [Blockly.Msg.image_process_set_filled_screen_color_cyan, "cyan"],
- [Blockly.Msg.image_process_set_filled_screen_color_yellow, "yellow"],
- [Blockly.Msg.image_process_set_filled_screen_color_white, "white"]
- ]), "COLOR");
- this.setOutput(true, "String");
- this.setInputsInline(true);
- this.setColour(Turtle_HUE);
- this.setTooltip('');
- this.setHelpUrl('');
- }
- }
- Blockly.Python['turtle_color_set'] = function (block) {
- var color = block.getFieldValue('COLOR');
- var code = '(\"' + color + '\")';
- // TODO: Assemble Python into code variable.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['turtle_color'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.TURTLE_MAKE_TURRLE_COLOR);
- this.appendValueInput("COLOR")
- .setCheck(null)
- .appendField("");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setOutput(false);
- this.setInputsInline(true);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURRLE_COLOR_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_color'] = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.pencolor(' + color + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_forward'] = {
- init: function () {
- this.appendValueInput("DISTANCE")
- .setCheck(null)
- .appendField(Blockly.Msg.TURTLE_MAKE_TURTLE_MOVE_FORWARD_BY);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURTLE_MOVE_FORWARD_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_forward'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle' + '.forward(' + distance + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_backward'] = {
- init: function () {
- this.appendValueInput("DISTANCE")
- .setCheck(null)
- .appendField(Blockly.Msg.TURTLE_MAKE_TURTLE_MOVE_BACKWARD_BY);
- this.setPreviousStatement(true);
- this.setInputsInline(true);
- this.setNextStatement(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURTLE_MOVE_BACKWARD_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_backward'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var distance = Blockly.Python.valueToCode(block, 'DISTANCE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.backward(' + distance + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_left'] = {
- init: function () {
- this.appendValueInput("ANGLE")
- .setCheck(null)
- .appendField(Blockly.Msg.TURTLE_MAKE_TURN_LEFT_BY);
- this.appendDummyInput("")
- .appendField(Blockly.Msg.TURTLE_DEGREE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURN_LEFT_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_left'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.left(' + angle + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_right'] = {
- init: function () {
- this.appendValueInput("ANGLE")
- .setCheck(null)
- .appendField(Blockly.Msg.TURTLE_MAKE_TURN_RIGHT_BY);
- this.appendDummyInput("")
- .appendField(Blockly.Msg.TURTLE_DEGREE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURN_RIGHT_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_right'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.right(' + angle + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_pen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.TURTLE_MAKE_TURRLE_PEN)
- .appendField(new Blockly.FieldDropdown([[Blockly.Msg.TURTLE_PEN_UP, "up"], [Blockly.Msg.TURTLE_PEN_DOWN, "down"]]), "Pen");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(Turtle_HUE);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('Pen');
- var TOOLTIPS = {
- 'up': Blockly.Msg.TURTLE_MAKE_TURRLE_PEN_TOOLTIP.replace('%1', Blockly.Msg.TURTLE_PEN_UP),
- 'down': Blockly.Msg.TURTLE_MAKE_TURRLE_PEN_TOOLTIP.replace('%1', Blockly.Msg.TURTLE_PEN_DOWN)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['turtle_pen'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var pen = block.getFieldValue('Pen');
- // TODO: Assemble Python into code variable.
- var code = 'turtle.pen' + pen + '()\n';
- return code;
- };
- Blockly.Blocks['turtle_goto'] = {
- init: function () {
- this.appendValueInput("X")
- .setCheck("Number")
- .appendField(Blockly.Msg.TURTLE_MAKE_TURTLE_GOTO_LOCATION_X);
- this.appendValueInput("Y")
- .setCheck("Number")
- .appendField(", Y:");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURTLE_GOTO_LOCATION_X_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['turtle_goto'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var x_coordinate = Blockly.Python.valueToCode(block, 'X', Blockly.Python.ORDER_ATOMIC);
- var y_coordinate = Blockly.Python.valueToCode(block, 'Y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.goto(' + x_coordinate + ',' + y_coordinate + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_set_pos'] = {
- init: function () {
- this.appendValueInput("X")
- .setCheck("Number")
- .appendField(Blockly.Msg.TURTLE_MAKE_TURTLE_SET_POSITION);
- this.appendValueInput("Y")
- .setCheck("Number")
- .appendField(", Y:");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_TURTLE_SET_POSITION_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['turtle_set_pos'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var x_coordinate = Blockly.Python.valueToCode(block, 'X', Blockly.Python.ORDER_ATOMIC);
- var y_coordinate = Blockly.Python.valueToCode(block, 'Y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.setPosition(' + x_coordinate + ',' + y_coordinate + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_setheading'] = {
- init: function () {
- this.appendValueInput("ANGLE")
- .setCheck(null)
- .appendField(Blockly.Msg.TURTLE_MAKE_SET_HEADING);
- this.appendDummyInput("")
- .appendField(Blockly.Msg.TURTLE_DEGREE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_MAKE_SET_HEADING_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_setheading'] = function (block) {
- //var turtle = Blockly.Python.valueToCode(block, 'TURTLE', Blockly.Python.ORDER_ATOMIC);
- var angle = Blockly.Python.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'turtle.setheading(' + angle + ')\n';
- return code;
- };
- Blockly.Blocks['turtle_reset'] = {
- init: function () {
- this.appendDummyInput("")
- .appendField(Blockly.Msg.TURTLE_RESET);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_RESET_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_reset'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'turtle.reset()\n';
- return code;
- };
- Blockly.Blocks['turtle_clear'] = {
- init: function () {
- this.appendDummyInput("")
- .appendField(Blockly.Msg.TURTLE_CLEAR);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setInputsInline(true);
- this.setOutput(false);
- this.setColour(Turtle_HUE);
- this.setTooltip(Blockly.Msg.TURTLE_CLEAR_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['turtle_clear'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'turtle.clear()\n';
- return code;
- };
- // 室温传感器
- Blockly.Blocks['v831_room_water_temperature'] = {
- init: function () {
- this.appendDummyInput("")
- .appendField("IIC" + Blockly.Msg.room_water_temperature_text);
- // this.setInputsInline(true);
- this.setOutput(true);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.room_water_temperature_text);
- this.setHelpUrl('');
- }
- };
- Blockly.Python['v831_room_water_temperature'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_sys'] = 'import sys';
- Blockly.Python.definitions_['v831_import_serial'] = 'import serial';
- Blockly.Python.definitions_['v831_room_water_temperature_init'] = `class Ds18b20():
- SEARCH_ROM = 0xf0
- READ_ROM = 0x33
- MATCH_ROM = 0x55
- SKIP_ROM_BROADCAST = 0xcc
- ALARM_SEARCH = 0xec
- TEMPERATURE_READ = 0x44
- READ_SCRATCHPAD = 0xbe
- WRITE_SCRATCHPAD = 0x4e
- COPY_SCRATCHPAD = 0x48
- RECALL_EEPROM = 0xb8
- READ_POWER_SUPPLY = 0xb4
- def __init__(self, port=None):
- self.ser = None
- if port is not None:
- self.port = port
- else:
- self.port = None
- self.connect()
- def connect(self):
- self.port="/dev/ttyS1"
- self.ser = serial.Serial()
- self.ser.port = self.port
- self.ser.baudrate = 115200
- self.ser.bytesize = 8
- self.ser.parity = 'N'
- self.ser.stopbits = 1
- self.ser.timeout = 0.1
- self.ser.open()
- if not self.ser.is_open:
- print("Failed to open serial port")
- sys.exit(1)
- def close(self):
- self.ser.close()
- def reset(self):
- self.ser.baudrate = 9600
- self.ser.write(b'\\xF0')
- output = self.ser.read()
- if output == b'\\xF0':
- raise Exception("No devices found")
- elif output == b'\\x00':
- raise Exception("Shorted?")
- elif output == b'\\xe0':
- # print("Single device")
- pass
- else:
- # print("Multiple devices?")
- pass
- self.ser.baudrate = 115200
- def send_byte(self, data):
- output = 0
- for i in range(8):
- bit = (data >> i) & 1
- if bit == 1:
- self.ser.write(b'\\xff')
- else:
- self.ser.write(b'\\x00')
- outbit = self.ser.read()
- if outbit == b'\\xFF':
- output += (1 << i)
- return output
- def send_command(self, target, command):
- self.reset()
- self.send_byte(target)
- return self.send_byte(command)
- def do_a_temperature_read(self):
- self.send_command(Ds18b20.SKIP_ROM_BROADCAST, Ds18b20.TEMPERATURE_READ)
- while True:
- if self.send_byte(0xFF) > 0:
- break
- time.sleep(0.5)
- def get_scratchpad(self):
- output = {}
- self.send_command(Ds18b20.SKIP_ROM_BROADCAST, Ds18b20.READ_SCRATCHPAD)
- scratchpad = []
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- scratchpad.append(self.send_byte(0xFF))
- output["temperature_lsb"] = scratchpad[0]
- output["temperature_msb"] = scratchpad[1]
- output["th_register"] = scratchpad[2]
- output["tl_register"] = scratchpad[3]
- output["configuration"] = scratchpad[4]
- output["reserved_ff"] = scratchpad[5]
- output["reserved_xx"] = scratchpad[6]
- output["reserved_10"] = scratchpad[7]
- output["crc"] = scratchpad[8]
- r0 = (output["configuration"] >> 5) & 1
- r1 = (output["configuration"] >> 6) & 1
- r = r0 + (r1 << 1)
- output["resolution"] = 9 + r
- return output
- def write_scratchpad(self, th, tl, config):
- self.send_command(Ds18b20.SKIP_ROM_BROADCAST, Ds18b20.WRITE_SCRATCHPAD)
- self.send_byte(th)
- self.send_byte(tl)
- self.send_byte(config)
- def get_temperature(self):
- self.do_a_temperature_read()
- self.send_command(Ds18b20.SKIP_ROM_BROADCAST, Ds18b20.READ_SCRATCHPAD)
- temperature = self.send_byte(0xFF)
- temperature += self.send_byte(0xFF) << 8
- temperature *= 0.0625
- return temperature
- def get_single_rom_code(self):
- rom_code = []
- rom_code_bytes = b''
- self.reset()
- self.send_byte(Ds18b20.READ_ROM)
- for _ in range(8):
- abyte = self.send_byte(0xff)
- rom_code_bytes = bytes([abyte]) + rom_code_bytes
- rom_code = [abyte] + rom_code
- crc = rom_code[0]
- serial = rom_code[1:7]
- serial_bytes = rom_code_bytes[1:7]
- family_code = rom_code[7]
- return serial_bytes
- def get_power_supply(self):
- self.send_command(Ds18b20.SKIP_ROM_BROADCAST, Ds18b20.READ_POWER_SUPPLY)
- if self.send_byte(0xff) == 0xff:
- print("All devices connected to proper power")
- else:
- print("Some devices connected as parasitic")
- def set_accuracy(self, accuracy):
- if accuracy < 9 or accuracy > 12:
- raise Exception("Only an accuracy of 9-12 bits is allowed")
- scratchpad = ds.get_scratchpad() # get the current scratchpad data so we can pass tl and th the same as currently set
- set_accuracy = accuracy - 9
- set_accuracy = set_accuracy << 5
- new_config = (scratchpad["configuration"] & 0b10011111) | set_accuracy
- ds.write_scratchpad(th=scratchpad["th_register"], tl=scratchpad["tl_register"], config=new_config)
- ds = Ds18b20("/dev/ttyS1")
- `;
- // Blockly.Python.addFunction("getRoomWaterTemperature", `def getRoomWaterTemperature():
- // serial_num = ds.get_single_rom_code()
- // temperature = ds.get_temperature()
- // return temperature
- // `, true)
- var code = 'ds.get_temperature()';
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['v831_set_real_time_clock'] = {
- init: function () {
- let nowDate = new Date();
- this.appendDummyInput()
- .appendField("IIC" + Blockly.Msg.time_set_current_date_title)
- .appendField(new Blockly.FieldTextInput(nowDate.getFullYear()), "year")
- .appendField(Blockly.Msg.time_get_current_date_y)
- .appendField(new Blockly.FieldTextInput(nowDate.getMonth() + 1), "month")
- .appendField(Blockly.Msg.time_get_current_date_m)
- .appendField(new Blockly.FieldTextInput(nowDate.getDate()), "day")
- .appendField(Blockly.Msg.time_get_current_date_d)
- .appendField(Blockly.Msg.time_get_current_date_week)
- .appendField(new Blockly.FieldTextInput(nowDate.getDay()), "week")
- .appendField(new Blockly.FieldTextInput(nowDate.getHours()), "hour")
- .appendField(Blockly.Msg.time_get_current_date_hour)
- .appendField(new Blockly.FieldTextInput(nowDate.getMinutes()), "minutes")
- .appendField(Blockly.Msg.time_get_current_date_min)
- .appendField(new Blockly.FieldTextInput(nowDate.getSeconds()), "seconds")
- .appendField(Blockly.Msg.time_get_current_date_sec);
- this.setNextStatement(true);
- this.setPreviousStatement(true);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.time_set_current_date_title);
- }
- };
- Blockly.Python['v831_set_real_time_clock'] = function (block) {
- var year = block.getFieldValue('year');
- var month = block.getFieldValue('month');
- var day = block.getFieldValue('day');
- var week = block.getFieldValue('week');
- var hour = block.getFieldValue('hour');
- var minutes = block.getFieldValue('minutes');
- var seconds = block.getFieldValue('seconds');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = 'import smbus2';
- Blockly.Python.definitions_['v831_import_math'] = 'import math';
- Blockly.Python.definitions_['v831_import_time'] = 'import time';
- Blockly.Python.definitions_['v831_real_time_clock_init'] = `class DS3231(object):
- # create RTC instance
- def __init__(self):
- self.bus = smbus2.SMBus(2) # 2 indicates /dev/i2c-2
- self.address = 0x68
-
- def bcd_to_int(self,bcd, n=2):
- """Decode n least significant packed binary coded decimal digits to binary.
- Return binary result.
- n defaults to 2 (BCD digits).
- n=0 decodes all digits.
- """
- return int(('%x' % bcd)[-n:])
- def int_to_bcd(self,x, n=2):
- """
- Encode the n least significant decimal digits of x
- to packed binary coded decimal (BCD).
- Return packed BCD value.
- n defaults to 2 (digits).
- n=0 encodes all digits.
- """
- return int(str(x)[-n:], 0x10)
-
-
- def read(self, addr):
- for i in range(0, 3):
- try:
- tmp = self.bus.read_byte_data(self.address, addr)
- time.sleep(0.001) # 1ms
- # print(addr, tmp) # debug
- return tmp
- except Exception:
- time.sleep(0.01)
- continue
- return None
- def write(self, addr, val):
- for i in range(0, 3):
- try:
- self.bus.write_byte_data(self.address, addr, val)
- #time.sleep(0.001) # 1ms
- # print(addr, val) # debug
- return True
- except Exception:
- time.sleep(0.001)
- continue
- return False
- # set times functions ---------------------------------------------------------------------
- def setYear(self, year):
- # only last two digits (last two digits are used if longer)
- self.write(0x06, self.int_to_bcd(year%100))
- def setMonth(self, month):
- if not 1 <= month <= 12:
- raise ValueError('Month is out of range [1,12].')
- self.write(0x05,self.int_to_bcd(month))
-
- def setDay(self, day):
- if not 1 <= day <= 31:
- raise ValueError('Day is out of range [1,31].')
- self.write(0x04,self.int_to_bcd(day))
-
- def setDayOfWeek(self, dayOfWeek):
- if not 1 <= dayOfWeek <= 7:
- raise ValueError('Day of week is out of range [1,7].')
- self.write(0x03,self.int_to_bcd(dayOfWeek))
-
- def setHour(self, hour):
- if not 0 <= hour < 24:
- raise ValueError('Hour is out of range [0,23].')
- self.write(0x02,self.int_to_bcd(hour)& 0x3F)
-
- def setMinutes(self, minutes):
- if not 0 <= minutes < 59:
- raise ValueError('Minutes is out of range [0,59].')
- self.write(0x01,self.int_to_bcd(minutes))
-
- def setSeconds(self, seconds):
- if not 0 <= seconds < 60:
- raise ValueError('Seconds is out of range [0,59].')
- self.write(0x00,self.int_to_bcd(seconds))
-
- def setDateTime(self, year, month, day, dayOfWeek, hour, minutes, seconds):
- # set all the date and times (year is last two digits of year)
- self.setYear(year)
- self.setMonth(month)
- self.setDay(day)
- self.setDayOfWeek(dayOfWeek)
- self.setHour(hour)
- self.setMinutes(minutes)
- self.setSeconds(seconds)
- # get times functions -------------------------------------------------
- def getYear(self):
- return self.bcd_to_int(self.read(0x06))
-
- def getMonth(self):
- temp = self.read(0x05)
- return self.bcd_to_int((temp<<4>>4) &0x7F)
- #return temp[0] & 0x7F
-
- def getDay(self):
- # 0 - 31
- return self.bcd_to_int(self.read(0x04))
-
- def getDayOfWeek(self):
- # 1 - 7
- return self.bcd_to_int(self.read(0x03))
-
- def getHour(self):
- temp = self.read(0x02)
- if temp==0x64: #if hour is 24:00,convert it to 00:00
- temp=0x40
- return self.bcd_to_int((temp<< 4 >>4)& 0x3F)
- #return temp[0] & 0x3F
-
- def getMinutes(self):
- return self.bcd_to_int(self.read(0x01))
-
- def getSeconds(self):
- return self.bcd_to_int(self.read(0x00))
-
- def getDateTime(self):
- # returns whole date and time as list
- # (last two digits of year, month, day, day of week, hour, minutes, seconds)
- dateTime = [0, 0, 0, 0, 0, 0, 0]
- dateTime[0] = self.getYear()
- dateTime[1] = self.getMonth()
- dateTime[2] = self.getDay()
- dateTime[3] = self.getDayOfWeek()
- dateTime[4] = self.getHour()
- dateTime[5] = self.getMinutes()
- dateTime[6] = self.getSeconds()
- return dateTime
- def convertToByteType(self,number):
- return bytes([number])
- def decodeToDec(self,byte):
- return ((byte[0] >> 4) * 10) + (byte[0] & 0x0F)
- def encodeToByte(self,dec):
- tens = math.floor(dec / 10)
- ones = dec - tens*10
- return (tens << 4) + ones
- def decodeAlarmType(self,alarmTime):
- if(len(alarmTime) > 4):
- m1Bit = (alarmTime[3] & 0x80) >> 7
- else:
- m1Bit = False
- m2Bit = (alarmTime[2] & 0x80) >> 7
- m3Bit = (alarmTime[1] & 0x80) >> 7
- m4Bit = (alarmTime[0] & 0x80) >> 7
- dayBit = (alarmTime[0] & 0x40) >> 6
-
- if(m1Bit and m2Bit and m3Bit and m4Bit):
- return "everySecond"
- elif(not m1Bit and m2Bit and m3Bit and m4Bit):
- return "everyMinute"
- elif(not m1Bit and not m2Bit and m3Bit and m4Bit):
- return "everyHour"
- elif(not m1Bit and not m2Bit and not m3Bit and m4Bit):
- return "everyDay"
- elif(not dayBit and not m1Bit and not m2Bit and not m3Bit and not m4Bit):
- return "everyMonth"
- elif(dayBit and not m1Bit and not m2Bit and not m3Bit and not m4Bit):
- return "everyWeek"
- else:
- return "noValidAlarmType"
-
- def decodeAlarmTime(self,alarmTime):
- alarmTime[0] = decodeToDec(convertToByteType(alarmTime[0] & 0x3F))
- alarmTime[1] = decodeToDec(convertToByteType(alarmTime[1] & 0x3F))
- alarmTime[2] = decodeToDec(convertToByteType(alarmTime[2] & 0x7F))
- if(len(alarmTime) > 4):
- alarmTime[3] = decodeToDec(convertToByteType(alarmTime[3] & 0x7F))
- return alarmTime
- def encodeAlarmType(self,alarmType):
- if(alarmType == "everySecond"):
- return 15 #0b01111
- elif(alarmType == "everyMinute"):
- return 14 #0b01110
- elif(alarmType == "everyHour"):
- return 12 #0b01100
- elif(alarmType == "everyDay"):
- return 8 #0b01000
- elif(alarmType == "everyMonth"):
- return 0 #0b00000
- elif(alarmType == "everyWeek"):
- return 16 #0b10000
- else:
- raise ValueError("""Not a supported alarmType. Options are:
- 'everySecond' (only Alarm 1), 'everyMinute', 'everyHour', 'everyDay', 'everyMonth', 'everyWeek'""")
- def encodeDateTime(self,day, hour, minutes, seconds, alarmType):
- alarmBits = encodeAlarmType(alarmType)
- alarmTime = [0, 0, 0, 0]
- alarmTime[0] = (encodeToByte(day) & 0x3F) | ((alarmBits & 0x10) << 2) | ((alarmBits & 0x08) << 4)
- alarmTime[1] = (encodeToByte(hour) & 0x3F) | ((alarmBits & 0x04) << 5)
- alarmTime[2] = (encodeToByte(minutes) & 0x7F) | ((alarmBits & 0x02) << 6)
- alarmTime[3] = (encodeToByte(seconds) & 0x7F) | ((alarmBits & 0x01) << 7)
- return alarmTime
-
- rtc=DS3231()
- `;
- var code = `rtc.setYear(${year})
- rtc.setMonth(${month})
- rtc.setDay(${day})
- rtc.setDayOfWeek(${week})
- rtc.setHour(${hour})
- rtc.setMinutes(${minutes})
- rtc.setSeconds(${seconds})\n`;
- return code;
- };
- // v831_real_time_clock
- Blockly.Blocks['v831_real_time_clock'] = {
- init: function () {
- this.appendDummyInput()
- .appendField("IIC" + Blockly.Msg.RTC_getDateTime)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_get_current_date_y, "getYear"],
- [Blockly.Msg.time_get_current_date_m, "getMonth"],
- [Blockly.Msg.time_get_current_date_d, "getDay"],
- [Blockly.Msg.RTC_SETWEEK, "getDayOfWeek"],
- [Blockly.Msg.time_get_current_date_hour, "getHour"],
- [Blockly.Msg.time_get_current_date_min, "getMinutes"],
- [Blockly.Msg.time_get_current_date_sec, "getSeconds"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.RTC_getDateTime);
- }
- };
- Blockly.Python['v831_real_time_clock'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = 'import smbus2';
- Blockly.Python.definitions_['v831_import_math'] = 'import math';
- Blockly.Python.definitions_['v831_import_time'] = 'import time';
- Blockly.Python.definitions_['v831_real_time_clock_init'] = `class DS3231(object):
- # create RTC instance
- def __init__(self):
- self.bus = smbus2.SMBus(2) # 2 indicates /dev/i2c-2
- self.address = 0x68
-
- def bcd_to_int(self,bcd, n=2):
- """Decode n least significant packed binary coded decimal digits to binary.
- Return binary result.
- n defaults to 2 (BCD digits).
- n=0 decodes all digits.
- """
- return int(('%x' % bcd)[-n:])
- def int_to_bcd(self,x, n=2):
- """
- Encode the n least significant decimal digits of x
- to packed binary coded decimal (BCD).
- Return packed BCD value.
- n defaults to 2 (digits).
- n=0 encodes all digits.
- """
- return int(str(x)[-n:], 0x10)
-
-
- def read(self, addr):
- for i in range(0, 3):
- try:
- tmp = self.bus.read_byte_data(self.address, addr)
- time.sleep(0.001) # 1ms
- # print(addr, tmp) # debug
- return tmp
- except Exception:
- time.sleep(0.01)
- continue
- return None
- def write(self, addr, val):
- for i in range(0, 3):
- try:
- self.bus.write_byte_data(self.address, addr, val)
- #time.sleep(0.001) # 1ms
- # print(addr, val) # debug
- return True
- except Exception:
- time.sleep(0.001)
- continue
- return False
- # set times functions ---------------------------------------------------------------------
- def setYear(self, year):
- # only last two digits (last two digits are used if longer)
- self.write(0x06, self.int_to_bcd(year%100))
- def setMonth(self, month):
- if not 1 <= month <= 12:
- raise ValueError('Month is out of range [1,12].')
- self.write(0x05,self.int_to_bcd(month))
-
- def setDay(self, day):
- if not 1 <= day <= 31:
- raise ValueError('Day is out of range [1,31].')
- self.write(0x04,self.int_to_bcd(day))
-
- def setDayOfWeek(self, dayOfWeek):
- if not 1 <= dayOfWeek <= 7:
- raise ValueError('Day of week is out of range [1,7].')
- self.write(0x03,self.int_to_bcd(dayOfWeek))
-
- def setHour(self, hour):
- if not 0 <= hour < 24:
- raise ValueError('Hour is out of range [0,23].')
- self.write(0x02,self.int_to_bcd(hour)& 0x3F)
-
- def setMinutes(self, minutes):
- if not 0 <= minutes < 59:
- raise ValueError('Minutes is out of range [0,59].')
- self.write(0x01,self.int_to_bcd(minutes))
-
- def setSeconds(self, seconds):
- if not 0 <= seconds < 60:
- raise ValueError('Seconds is out of range [0,59].')
- self.write(0x00,self.int_to_bcd(seconds))
-
- def setDateTime(self, year, month, day, dayOfWeek, hour, minutes, seconds):
- # set all the date and times (year is last two digits of year)
- self.setYear(year)
- self.setMonth(month)
- self.setDay(day)
- self.setDayOfWeek(dayOfWeek)
- self.setHour(hour)
- self.setMinutes(minutes)
- self.setSeconds(seconds)
- # get times functions -------------------------------------------------
- def getYear(self):
- return self.bcd_to_int(self.read(0x06))
-
- def getMonth(self):
- temp = self.read(0x05)
- return self.bcd_to_int((temp<<4>>4) &0x7F)
- #return temp[0] & 0x7F
-
- def getDay(self):
- # 0 - 31
- return self.bcd_to_int(self.read(0x04))
-
- def getDayOfWeek(self):
- # 1 - 7
- return self.bcd_to_int(self.read(0x03))
-
- def getHour(self):
- temp = self.read(0x02)
- if temp==0x64: #if hour is 24:00,convert it to 00:00
- temp=0x40
- return self.bcd_to_int((temp<< 4 >>4)& 0x3F)
- #return temp[0] & 0x3F
-
- def getMinutes(self):
- return self.bcd_to_int(self.read(0x01))
-
- def getSeconds(self):
- return self.bcd_to_int(self.read(0x00))
-
- def getDateTime(self):
- # returns whole date and time as list
- # (last two digits of year, month, day, day of week, hour, minutes, seconds)
- dateTime = [0, 0, 0, 0, 0, 0, 0]
- dateTime[0] = self.getYear()
- dateTime[1] = self.getMonth()
- dateTime[2] = self.getDay()
- dateTime[3] = self.getDayOfWeek()
- dateTime[4] = self.getHour()
- dateTime[5] = self.getMinutes()
- dateTime[6] = self.getSeconds()
- return dateTime
- def convertToByteType(self,number):
- return bytes([number])
- def decodeToDec(self,byte):
- return ((byte[0] >> 4) * 10) + (byte[0] & 0x0F)
- def encodeToByte(self,dec):
- tens = math.floor(dec / 10)
- ones = dec - tens*10
- return (tens << 4) + ones
- def decodeAlarmType(self,alarmTime):
- if(len(alarmTime) > 4):
- m1Bit = (alarmTime[3] & 0x80) >> 7
- else:
- m1Bit = False
- m2Bit = (alarmTime[2] & 0x80) >> 7
- m3Bit = (alarmTime[1] & 0x80) >> 7
- m4Bit = (alarmTime[0] & 0x80) >> 7
- dayBit = (alarmTime[0] & 0x40) >> 6
-
- if(m1Bit and m2Bit and m3Bit and m4Bit):
- return "everySecond"
- elif(not m1Bit and m2Bit and m3Bit and m4Bit):
- return "everyMinute"
- elif(not m1Bit and not m2Bit and m3Bit and m4Bit):
- return "everyHour"
- elif(not m1Bit and not m2Bit and not m3Bit and m4Bit):
- return "everyDay"
- elif(not dayBit and not m1Bit and not m2Bit and not m3Bit and not m4Bit):
- return "everyMonth"
- elif(dayBit and not m1Bit and not m2Bit and not m3Bit and not m4Bit):
- return "everyWeek"
- else:
- return "noValidAlarmType"
-
- def decodeAlarmTime(self,alarmTime):
- alarmTime[0] = decodeToDec(convertToByteType(alarmTime[0] & 0x3F))
- alarmTime[1] = decodeToDec(convertToByteType(alarmTime[1] & 0x3F))
- alarmTime[2] = decodeToDec(convertToByteType(alarmTime[2] & 0x7F))
- if(len(alarmTime) > 4):
- alarmTime[3] = decodeToDec(convertToByteType(alarmTime[3] & 0x7F))
- return alarmTime
- def encodeAlarmType(self,alarmType):
- if(alarmType == "everySecond"):
- return 15 #0b01111
- elif(alarmType == "everyMinute"):
- return 14 #0b01110
- elif(alarmType == "everyHour"):
- return 12 #0b01100
- elif(alarmType == "everyDay"):
- return 8 #0b01000
- elif(alarmType == "everyMonth"):
- return 0 #0b00000
- elif(alarmType == "everyWeek"):
- return 16 #0b10000
- else:
- raise ValueError("""Not a supported alarmType. Options are:
- 'everySecond' (only Alarm 1), 'everyMinute', 'everyHour', 'everyDay', 'everyMonth', 'everyWeek'""")
- def encodeDateTime(self,day, hour, minutes, seconds, alarmType):
- alarmBits = encodeAlarmType(alarmType)
- alarmTime = [0, 0, 0, 0]
- alarmTime[0] = (encodeToByte(day) & 0x3F) | ((alarmBits & 0x10) << 2) | ((alarmBits & 0x08) << 4)
- alarmTime[1] = (encodeToByte(hour) & 0x3F) | ((alarmBits & 0x04) << 5)
- alarmTime[2] = (encodeToByte(minutes) & 0x7F) | ((alarmBits & 0x02) << 6)
- alarmTime[3] = (encodeToByte(seconds) & 0x7F) | ((alarmBits & 0x01) << 7)
- return alarmTime
-
- rtc=DS3231()
- `;
- var code = `rtc.${dropdown_type}()`;
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
|