1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080 |
- /* 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"]
- // ["12", "12"]
- ]), "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} {level} > /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 1
- else:
- return 0
- `)
- 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"]
- ]), "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} {level} > /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(`pinModeOut${value_name}`, `pinMode("${value_name}","1")`, 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"]
- // ["3", "3"],
- // ["4", "4"],
- // ["5", "5"],
- // ["6", "6"],
- // ["7", "7"],
- // ["8", "8"],
- // ["9", "9"],
- // ["10", "10"],
- // ["11", "11"],
- // ["12", "12"]
- ]), "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/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- if (value_gpio == 1 || value_gpio == 2) {
- Blockly.Python.addVariable('_PWN_gpio_' + value_gpio, `_PWN_gpio_${value_gpio} = multiFuncGpio(${value_gpio - 1},4)`, true);
- } else {
- Blockly.Python.addVariable('_PWN_gpio_' + value_gpio, `_PWN_gpio_${value_gpio} = multiFuncGpio(${value_gpio - 1},4)`, true);
- }
- var code = `_PWN_gpio_${value_gpio}.analogRead()`;
- // 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"]
- // ["3", "3"],
- // ["4", "4"],
- // ["5", "5"],
- // ["6", "6"],
- // ["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/")`
- 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);
- var code = `PWNOUT_${value_name1}.analogWrite(${value_name2})`;
- 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_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_QMI8658'] = `from CocoPi import LIGHTINTENSITY`
- Blockly.Python.definitions_['v831_import_LIGHTINTENSITY'] = `v831_adc0 = LIGHTINTENSITY()
- `
- 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["V831_code_scanning_network"] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.V831_code_scanning_network);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.V831_code_scanning_network);
- this.setHelpUrl("");
- }
- }
- Blockly.Python["V831_code_scanning_network"] = function (block) {
- Blockly.Python.definitions_['v831_import_mjpg'] = `from maix import mjpg`
- Blockly.Python.definitions_['v831_import_utils'] = `from maix import utils`
- Blockly.Python.definitions_['v831_import_base64'] = `import base64`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
- Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
- Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
- Blockly.Python.definitions_['lcd_invert_init_new'] = `ScreenOrientation = False
- def lcdRotationNew(inputImg):
- global SETVFLIP,SETHMIRROT,ScreenOrientation
- imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
- if ScreenOrientation:
- imgRotationAim = image.new(size = (240, 320))
- rotationAngle = 90
- GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
- else:
- imgRotationAim = image.new(size = (320, 240))
- GETROTATION = imageRotationBuffer
-
- GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
- return GETROTATION
- `
- 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
- `
- 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
- `
- let language = localStorage.getItem('handPyLanguage')
- let str = ""
- if (language == "zh-hant") {
- str = "請掃描平臺Wi-Fi二維碼聯網"
- } else if (language == "en") {
- str = "Please scan the platform Wi-Fi QR code for networking"
- } else {
- str = "请扫描平台Wi-Fi二维码联网"
- }
- Blockly.Python.definitions_[`v831_display_show_canvas`] = `def v831_display_show_canvas(displayShow,_canvas_x,_canvas_y):
- global ScreenOrientation,cameraSize
- try:
- if ScreenOrientation:
- displayShowCanvas = image.new(size = (240, 320))
- displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
- displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
- displayShowVER = displayShowCanvas.crop(0,0,240,320)
- displayShowVER = displayShowVER.rotate(-90, adjust=1)
- display.show(displayShowVER)
- else:
- displayShowCanvas = image.new(size = (320, 240))
- displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
- displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
- display.show(displayShowCanvas)
- except:
- displayShowCanvas = image.new(size = (320, 240))
- displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
- displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
- display.show(displayShowCanvas)
-
- `
- Blockly.Python.addFunction("codeScanningNetwork", `def codeScanningNetwork():
- if wifi_is_content():
- stateNetwork = 1
- else:
- stateNetwork = 0
- CODERESULT = ""
- while True:
- if stateNetwork == 0:
- canvas = lcdRotationNew(camera.capture())
- qrCode = canvas.find_qrcodes()
- canvas.draw_rectangle(0,0, 0+320,0+ 20, color=(0,0,0), thickness=-1)
- canvas.draw_string(50,0, "${str}", scale = 1, color = (255,255,255) , thickness = 1)
- for i in qrCode:
- canvas.draw_string((i["x"]),(i["y"]), (i["payload"]), scale = 1, color = (255,0,0) , thickness = 1)
- canvas.draw_rectangle((i["x"]),(i["y"]), (i["x"])+(i["w"]),(i["y"])+ (i["h"]), color=(255,0,0), thickness=1)
- CODERESULT = (i["payload"]).split(";")
- v831_display_show_canvas(canvas,0,0)
- if CODERESULT != "":
- WiFiSSID = CODERESULT[0][5 : ]
- WiFiPSD = CODERESULT[1][5 : ]
- os.system("wifi_disconnect_ap_test")
- os.system("wifi_connect_chinese_ap_test "+WiFiSSID+" "+WiFiPSD+"")
- getNetworkDate_noexit()
- if wifi_is_content():
- stateNetwork = 1
- else:
- stateNetwork = 0
- CODERESULT = ""
- elif stateNetwork == 1:
- break
- `
- )
- var code = `image.load_freetype("/root/preset/fonts/simhei.ttf")
- codeScanningNetwork()\n`
- 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.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_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 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):
- params = {
- "email": email,
- "content": content,
- "subject":"训练平台",
- "content_type":'plain'
- }
- emailURL = "https://traininguser-api.cocorobo.cn/sendEmail"
- try:
- r = requests.post(emailURL,json=params)
- print(json.loads(r.text))
- if json.loads(r.text)["FunctionResponse"] == "发送成功":
- return True
- else:
- return False
- except Exception as e:
- print(e)
- return False
- `)
- var code = `send_email(${Email},${EmailContent})`;
- return [code, Blockly.Python.ORDER_NONE];
- };
- 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_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['esp32_network_http_server_setup_def_setup'] = '' +
- 'from microWebSrv import MicroWebSrv\n' +
- '';
- 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'";
- }
- /*Blockly.Python.addVariable('', '' +
- '' +
- '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
- 'def _httpHandlerTestGet(httpClient, httpResponse):\n' +
- statements_exec +
- '', true);*/
- Blockly.Python.definitions_['esp32_network_http_server_route_def_' + text_path.substring(1)] = '' +
- '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
- 'def _httpHandler' + text_path.substring(1) + 'Get(httpClient, httpResponse):\n' +
- statements_exec +
- '\n';
- Blockly.Python.addSetup("server_route_setup", '' +
- 'def _acceptWebSocketCallback(webSocket, httpClient) :\n' +
- ' print("WS ACCEPT")\n' +
- ' webSocket.RecvTextCallback = _recvTextCallback\n' +
- ' webSocket.RecvBinaryCallback = _recvBinaryCallback\n' +
- ' webSocket.ClosedCallback = _closedCallback\n' +
- '\n' +
- 'def _recvTextCallback(webSocket, msg) :\n' +
- ' print("WS RECV TEXT : %s" % msg)\n' +
- ' webSocket.SendText("Reply for %s" % msg)\n' +
- '\n' +
- 'def _recvBinaryCallback(webSocket, data) :\n' +
- ' print("WS RECV DATA : %s" % data)\n' +
- '\n' +
- 'def _closedCallback(webSocket) :\n' +
- ' print("WS CLOSED")\n' +
- '\n' +
- 'try:\n' +
- ' srv = MicroWebSrv()\n' +
- ' srv.Stop()\n' +
- ' srv.MaxWebSocketRecvLen = 1024\n' +
- ' srv.WebSocketThreaded = False\n' +
- ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
- ' srv.Start(threaded=True)\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' srv = MicroWebSrv()\n' +
- ' srv.MaxWebSocketRecvLen = 1024\n' +
- ' srv.WebSocketThreaded = False\n' +
- ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
- ' srv.Start(threaded=True)\n' +
- '');
- // TODO: Assemble Python into code variable.
- var code = '';
- 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];
- };
|