ai.js 354 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533
  1. /*
  2. _ ___ ____ _
  3. / \ |_ _| | __ ) __ _ ___(_) ___
  4. / _ \ | | | _ \ / _` / __| |/ __|
  5. / ___ \ | | | |_) | (_| \__ \ | (__
  6. /_/ \_\___| |____/ \__,_|___/_|\___|
  7. */
  8. Blockly.Blocks['ai_k210_button_read_pressed'] = {
  9. init: function () {
  10. this.appendDummyInput()
  11. .appendField(Blockly.Msg.ai_basic_button_when)
  12. .appendField(new Blockly.FieldDropdown([
  13. ["A", "a"],
  14. ["B", "b"],
  15. ["C", "c"]
  16. ]), "button_type")
  17. .appendField(Blockly.Msg.ai_basic_pressed)
  18. this.setOutput(true, null);
  19. this.setColour("#d42b03");
  20. var thisBlock = this;
  21. this.setTooltip(function () {
  22. var mode = thisBlock.getFieldValue('button_type');
  23. var TOOLTIPS = {
  24. 'a': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  25. 'b': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  26. 'c': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C")
  27. };
  28. return TOOLTIPS[mode];
  29. });
  30. this.setHelpUrl("");
  31. }
  32. };
  33. Blockly.Python['ai_k210_button_read_pressed'] = function (block) {
  34. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  35. 'from fpioa_manager import *\n' +
  36. 'from Maix import FPIOA, GPIO\n' +
  37. '\n' +
  38. '_gp_side_buttons = [9, 10, 11]\n' +
  39. '\n' +
  40. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  41. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  42. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  43. '\n' +
  44. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  45. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  46. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  47. '\n';
  48. var _type = block.getFieldValue('button_type');
  49. var code = '_gp_side_' + _type + '.value() == 1';
  50. return [code, Blockly.Python.ORDER_CONDITIONAL];
  51. };
  52. Blockly.Blocks['ai_k210_button_read_released'] = {
  53. init: function () {
  54. this.appendDummyInput()
  55. .appendField(Blockly.Msg.ai_basic_button_when)
  56. .appendField(new Blockly.FieldDropdown([
  57. ["A", "a"],
  58. ["B", "b"],
  59. ["C", "c"]
  60. ]), "button_type_released")
  61. .appendField(Blockly.Msg.ai_basic_released)
  62. this.setOutput(true, null);
  63. this.setColour("#d42b03");
  64. var thisBlock = this;
  65. this.setTooltip(function () {
  66. var mode = thisBlock.getFieldValue('button_type_released');
  67. var TOOLTIPS = {
  68. 'a': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "A"),
  69. 'b': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "B"),
  70. 'c': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "C")
  71. };
  72. return TOOLTIPS[mode];
  73. });
  74. this.setHelpUrl("");
  75. }
  76. };
  77. Blockly.Python['ai_k210_button_read_released'] = function (block) {
  78. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  79. 'from fpioa_manager import *\n' +
  80. 'from Maix import FPIOA, GPIO\n' +
  81. '\n' +
  82. '_gp_side_buttons = [9, 10, 11]\n' +
  83. '\n' +
  84. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  85. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  86. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  87. '\n' +
  88. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  89. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  90. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  91. '\n';
  92. var _type = block.getFieldValue('button_type_released');
  93. var code = '_gp_side_' + _type + '.value() == 0';
  94. return [code, Blockly.Python.ORDER_CONDITIONAL];
  95. };
  96. Blockly.Blocks['ai_k210_led_light_up'] = {
  97. init: function () {
  98. this.appendDummyInput()
  99. .appendField(Blockly.Msg.ai_basic_led_set)
  100. .appendField(new Blockly.FieldDropdown([
  101. [Blockly.Msg.ai_basic_led_blue, "blue"],
  102. [Blockly.Msg.ai_basic_led_red, "red"]
  103. ]), "type")
  104. .appendField(Blockly.Msg.ai_basic_led_on);
  105. this.setPreviousStatement(true, null);
  106. this.setNextStatement(true, null);
  107. this.setColour("#d42b03");
  108. var thisBlock = this;
  109. this.setTooltip(function () {
  110. var mode = thisBlock.getFieldValue('type');
  111. var TOOLTIPS = {
  112. 'blue': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
  113. 'red': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
  114. };
  115. return TOOLTIPS[mode];
  116. });
  117. this.setHelpUrl("");
  118. }
  119. };
  120. Blockly.Python['ai_k210_led_light_up'] = function (block) {
  121. var dropdown_type = block.getFieldValue('type');
  122. Blockly.Python.definitions_['k210_led_light_def'] = '' +
  123. 'from fpioa_manager import *\n' +
  124. 'from Maix import GPIO\n' +
  125. '\n' +
  126. 'fm.register(31, fm.fpioa.GPIO3)\n' +
  127. 'fm.register(32, fm.fpioa.GPIO4)\n' +
  128. '\n' +
  129. '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
  130. '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
  131. '\n';
  132. // TODO: Assemble Python into code variable.
  133. var code = '_led_' + dropdown_type + '.value(1)\n';
  134. return code;
  135. };
  136. Blockly.Blocks['ai_k210_led_light_off'] = {
  137. init: function () {
  138. this.appendDummyInput()
  139. .appendField(Blockly.Msg.ai_basic_led_set)
  140. .appendField(new Blockly.FieldDropdown([
  141. [Blockly.Msg.ai_basic_led_blue, "blue"],
  142. [Blockly.Msg.ai_basic_led_red, "red"]
  143. ]), "type")
  144. .appendField(Blockly.Msg.ai_basic_led_off);
  145. this.setPreviousStatement(true, null);
  146. this.setNextStatement(true, null);
  147. this.setColour("#d42b03");
  148. var thisBlock = this;
  149. this.setTooltip(function () {
  150. var mode = thisBlock.getFieldValue('type');
  151. var TOOLTIPS = {
  152. 'blue': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
  153. 'red': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
  154. };
  155. return TOOLTIPS[mode];
  156. });
  157. this.setHelpUrl("");
  158. }
  159. };
  160. Blockly.Python['ai_k210_led_light_off'] = function (block) {
  161. var dropdown_type = block.getFieldValue('type');
  162. Blockly.Python.definitions_['k210_led_light_def'] = '' +
  163. 'from fpioa_manager import *\n' +
  164. 'from Maix import GPIO\n' +
  165. '\n' +
  166. 'fm.register(31, fm.fpioa.GPIO3)\n' +
  167. 'fm.register(32, fm.fpioa.GPIO4)\n' +
  168. '\n' +
  169. '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
  170. '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
  171. '\n';
  172. // TODO: Assemble Python into code variable.
  173. var code = '_led_' + dropdown_type + '.value(0)\n';
  174. return code;
  175. };
  176. Blockly.Blocks['ai_basic_light_up_led_analog'] = {
  177. init: function () {
  178. this.appendValueInput("brightness")
  179. .setCheck(null)
  180. .appendField(Blockly.Msg.ai_basic_led_analog_set)
  181. .appendField(new Blockly.FieldDropdown([
  182. [Blockly.Msg.ai_basic_led_analog_led1, "1"],
  183. [Blockly.Msg.ai_basic_led_analog_led2, "2"]
  184. ]), "type")
  185. .appendField(Blockly.Msg.ai_basic_led_analog_brightness_text);
  186. this.appendDummyInput()
  187. .appendField(Blockly.Msg.ai_basic_led_analog_range_text);
  188. this.setInputsInline(true);
  189. this.setPreviousStatement(true, null);
  190. this.setNextStatement(true, null);
  191. this.setColour("#d42b03");
  192. var thisBlock = this;
  193. this.setTooltip(function () {
  194. var mode = thisBlock.getFieldValue('type');
  195. var TOOLTIPS = {
  196. '1': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led1),
  197. '2': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led2)
  198. };
  199. return TOOLTIPS[mode];
  200. });
  201. this.setHelpUrl("");
  202. }
  203. };
  204. Blockly.Python['ai_basic_light_up_led_analog'] = function (block) {
  205. var dropdown_type = block.getFieldValue('type');
  206. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  207. Blockly.Python.definitions_['ai_basic_light_up_led_analog_def'] = '' +
  208. 'from machine import Timer,PWM\n' +
  209. 'import time\n' +
  210. '\n' +
  211. 'led_left_tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM)\n' +
  212. 'led_right_tim = Timer(Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM)\n' +
  213. 'led_left_ctrl = PWM(led_left_tim, freq=500000, duty=50, pin=31)\n' +
  214. 'led_right_ctrl = PWM(led_right_tim, freq=500000, duty=50, pin=32)\n' +
  215. '\n' +
  216. 'def analog_light(which, brightness):\n' +
  217. ' if which == 1:\n' +
  218. ' led_left_ctrl.duty(brightness)\n' +
  219. ' if which == 2:\n' +
  220. ' led_right_ctrl.duty(brightness)\n' +
  221. '';
  222. // TODO: Assemble Python into code variable.
  223. var code = 'analog_light(' + dropdown_type + ',' + value_brightness + ')\n';
  224. return code;
  225. };
  226. /*
  227. _ ___ ___ ____
  228. / \ |_ _| |_ _|_ __ ___ __ _ __ _ ___ | _ \ _ __ ___ ___ ___ ___ ___
  229. / _ \ | | | || '_ ` _ \ / _` |/ _` |/ _ \ | |_) | '__/ _ \ / __/ _ \/ __/ __|
  230. / ___ \ | | | || | | | | | (_| | (_| | __/ | __/| | | (_) | (_| __/\__ \__ \
  231. /_/ \_\___| |___|_| |_| |_|\__,_|\__, |\___| |_| |_| \___/ \___\___||___/___/
  232. |___/
  233. */
  234. Blockly.Blocks["ai_lcd_screeninit"] = {
  235. init: function () {
  236. this.appendDummyInput()
  237. .appendField(new Blockly.FieldImage("blockly/media/screen_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  238. this.appendDummyInput()
  239. .appendField(Blockly.Msg.image_process_lcd_init);
  240. this.setInputsInline(false);
  241. this.setPreviousStatement(true);
  242. this.setNextStatement(true);
  243. this.setColour("#5cb2d6");
  244. this.setTooltip(Blockly.Msg.ai_lcd_screeninit_TOOLTIP);
  245. this.setHelpUrl('');
  246. }
  247. }
  248. Blockly.Python.ai_lcd_screeninit = function (block) {
  249. var checkbox_name = block.getFieldValue('DEGREE');
  250. //var dropdown_name = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
  251. Blockly.Python.definitions_.import_lcd = "import lcd";
  252. Blockly.Python.definitions_.import_image = "import image";
  253. Blockly.Python.definitions_['import_define_x_ai_screeninit'] = '' +
  254. 'try:from cocorobo import display_cjk_string\n' +
  255. 'except:pass\n' +
  256. '\n' +
  257. 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n' +
  258. ' try: \n' +
  259. ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n' +
  260. ' return canvas\n' +
  261. ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n' +
  262. '\n';
  263. var _code = "lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n";
  264. _code += "lcd.rotation(1)\n";
  265. _code += "lcd.clear(lcd.BLACK)\n";
  266. return _code;;
  267. }
  268. Blockly.Blocks['ai_lcd_rotation'] = {
  269. init: function () {
  270. this.appendDummyInput()
  271. .appendField(Blockly.Msg.image_process_set_lcd_rotation)
  272. .appendField(new Blockly.FieldDropdown([
  273. ["0°", "1"],
  274. ["90°", "2"],
  275. ["180°", "3"],
  276. ["270°", "0"]
  277. ]), "DEGREE")
  278. this.setInputsInline(true);
  279. this.setPreviousStatement(true, null);
  280. this.setNextStatement(true, null);
  281. this.setColour("#5bb2d6");
  282. this.setTooltip(Blockly.Msg.ai_lcd_rotation_TOOLTIP);
  283. this.setHelpUrl('');
  284. }
  285. }
  286. Blockly.Python.ai_lcd_rotation = function (block) {
  287. var dropdown_name = block.getFieldValue('DEGREE');
  288. var _code = "lcd.rotation(" + dropdown_name + ")\n";
  289. return _code;;
  290. }
  291. Blockly.Blocks['ai_lcd_filp'] = {
  292. init: function () {
  293. this.appendDummyInput()
  294. .appendField(Blockly.Msg.image_process_set_lcd_mirror_flip)
  295. .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
  296. this.setInputsInline(true);
  297. this.setPreviousStatement(true, null);
  298. this.setNextStatement(true, null);
  299. this.setColour("#5bb2d6");
  300. this.setTooltip(Blockly.Msg.ai_lcd_filp_TOOLTIP);
  301. this.setHelpUrl('');
  302. }
  303. }
  304. Blockly.Python.ai_lcd_filp = function (block) {
  305. var _tf = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
  306. var _code = "lcd.mirror(" + _tf + ")\n";
  307. return _code;
  308. }
  309. Blockly.Blocks['ai_lcd_screen'] = {
  310. init: function () {
  311. this.appendDummyInput()
  312. .appendField(Blockly.Msg.image_process_set_filled_screen_color)
  313. .appendField(new Blockly.FieldDropdown([
  314. [Blockly.Msg.image_process_set_filled_screen_color_red, "RED"],
  315. [Blockly.Msg.image_process_set_filled_screen_color_green, "GREEN"],
  316. [Blockly.Msg.image_process_set_filled_screen_color_blue, "BLUE"],
  317. [Blockly.Msg.image_process_set_filled_screen_color_purple, "PURPLE"],
  318. [Blockly.Msg.image_process_set_filled_screen_color_cyan, "CYAN"],
  319. [Blockly.Msg.image_process_set_filled_screen_color_orange, "ORANGE"],
  320. [Blockly.Msg.image_process_set_filled_screen_color_yellow, "YELLOW"],
  321. [Blockly.Msg.image_process_set_filled_screen_color_black, "BLACK"],
  322. [Blockly.Msg.image_process_set_filled_screen_color_white, "WHITE"]
  323. ]), "COLOR");
  324. this.setInputsInline(true);
  325. this.setPreviousStatement(true, null);
  326. this.setNextStatement(true, null);
  327. this.setColour("#5bb2d6");
  328. var thisBlock = this;
  329. this.setTooltip(function () {
  330. var mode = thisBlock.getFieldValue('COLOR');
  331. var TOOLTIPS = {
  332. 'RED': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_red),
  333. 'GREEN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_green),
  334. 'BLUE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_blue),
  335. 'PURPLE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_purple),
  336. 'CYAN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_cyan),
  337. 'ORANGE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_orange),
  338. 'YELLOW': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_yellow),
  339. 'BLACK': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_black),
  340. 'WHITE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_white)
  341. };
  342. return TOOLTIPS[mode];
  343. });
  344. this.setHelpUrl('');
  345. }
  346. };
  347. Blockly.Python.ai_lcd_screen = function (block) {
  348. var colour_name = block.getFieldValue('COLOR');
  349. var _code = "lcd.clear(lcd." + colour_name + ")\n";
  350. return _code;
  351. }
  352. Blockly.Blocks['ai_lcd_fill_screen_with_rgb'] = {
  353. init: function () {
  354. this.appendDummyInput()
  355. .appendField(Blockly.Msg.image_process_set_filled_screen_color);
  356. this.appendValueInput("rgb_value")
  357. .setCheck(null)
  358. .appendField("");
  359. this.setInputsInline(true);
  360. this.setPreviousStatement(true, null);
  361. this.setNextStatement(true, null);
  362. this.setColour("#5bb2d6");
  363. this.setTooltip(Blockly.Msg.ai_lcd_fill_screen_with_rgb_TOOLTIP);
  364. this.setHelpUrl("");
  365. }
  366. };
  367. Blockly.Python['ai_lcd_fill_screen_with_rgb'] = function (block) {
  368. var value_name = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC);
  369. // TODO: Assemble Python into code variable.
  370. var code = 'lcd.clear(' + value_name + ')\n';
  371. return code;
  372. };
  373. Blockly.Blocks['ai_lcd_clear'] = {
  374. init: function () {
  375. this.appendDummyInput()
  376. .appendField(Blockly.Msg.image_process_clear_lcd);
  377. this.setInputsInline(true);
  378. this.setPreviousStatement(true, null);
  379. this.setNextStatement(true, null);
  380. this.setColour("#5bb2d6");
  381. this.setTooltip(Blockly.Msg.ai_lcd_clear_TOOLTIP);
  382. this.setHelpUrl('');
  383. }
  384. };
  385. Blockly.Python.ai_lcd_clear = function (block) {
  386. var _code = "lcd.clear(lcd.BLACK)\n";
  387. return _code;
  388. }
  389. Blockly.Blocks['ai_lcd_rgb_value_input'] = {
  390. init: function () {
  391. this.appendDummyInput()
  392. .appendField(Blockly.Msg.image_process_rgb_r);
  393. this.appendValueInput("rgb_value_r")
  394. .setCheck(null)
  395. .appendField("");
  396. this.appendDummyInput()
  397. .appendField(Blockly.Msg.image_process_rgb_g);
  398. this.appendValueInput("rgb_value_g")
  399. .setCheck(null)
  400. .appendField("");
  401. this.appendDummyInput()
  402. .appendField(Blockly.Msg.image_process_rgb_b);
  403. this.appendValueInput("rgb_value_b")
  404. .setCheck(null)
  405. .appendField("");
  406. this.setInputsInline(true);
  407. this.setOutput(true, null);
  408. this.setColour("#5bb2d6");
  409. this.setTooltip(Blockly.Msg.ai_lcd_rgb_value_input_TOOLTIP);
  410. this.setHelpUrl("");
  411. }
  412. };
  413. Blockly.Python['ai_lcd_rgb_value_input'] = function (block) {
  414. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  415. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  416. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  417. // TODO: Assemble Python into code variable.
  418. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  419. // TODO: Change ORDER_NONE to the correct strength.
  420. return [code, Blockly.Python.ORDER_NONE];
  421. };
  422. Blockly.Blocks.ai_lcd_XY = {
  423. init: function () {
  424. var _input = this.appendDummyInput();
  425. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  426. this.setTooltip(Blockly.Msg.ai_lcd_XY_TOOLTIP);
  427. this.setColour("#5bb2d6");
  428. this.setOutput(!0, "Boolean");
  429. _input.appendField(Blockly.Msg.image_process_xy_x);
  430. this.appendValueInput("A").setCheck("Number");
  431. _input = this.appendDummyInput();
  432. _input.appendField(Blockly.Msg.image_process_xy_y)
  433. this.appendValueInput("B").setCheck("Number");
  434. this.setInputsInline(!0);
  435. }
  436. };
  437. Blockly.Python.ai_lcd_XY = function (block) {
  438. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  439. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  440. var code = "" + _x + ", " + _y + "";
  441. return [code, Blockly.Python.ORDER_ATOMIC];
  442. };
  443. Blockly.Blocks.ai_lcd_WH = {
  444. init: function () {
  445. var _input = this.appendDummyInput();
  446. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  447. this.setTooltip(Blockly.Msg.ai_lcd_WH_TOOLTIP);
  448. this.setColour("#5bb2d6");
  449. this.setOutput(!0, "Boolean");
  450. _input.appendField(Blockly.Msg.image_process_wh_width);
  451. this.appendValueInput("A").setCheck("Number");
  452. _input = this.appendDummyInput();
  453. _input.appendField(Blockly.Msg.image_process_wh_height)
  454. this.appendValueInput("B").setCheck("Number");
  455. this.setInputsInline(!0);
  456. }
  457. };
  458. Blockly.Python.ai_lcd_WH = function (block) {
  459. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  460. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  461. var code = "" + _x + ", " + _y + "";
  462. return [code, Blockly.Python.ORDER_ATOMIC];
  463. };
  464. Blockly.Blocks['ai_lcd_colortext'] = {
  465. init: function () {
  466. this.appendDummyInput()
  467. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  468. this.appendDummyInput()
  469. .appendField(Blockly.Msg.image_process_lcd_show_text);
  470. this.appendDummyInput()
  471. .appendField(Blockly.Msg.image_process_lcd_show_text_color)
  472. .appendField(new Blockly.FieldDropdown([
  473. [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
  474. [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
  475. [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
  476. [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
  477. [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
  478. [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
  479. [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
  480. [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
  481. [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
  482. ]), "COLOR");
  483. this.appendDummyInput()
  484. .appendField(Blockly.Msg.image_process_lcd_show_text_bg_color)
  485. .appendField(new Blockly.FieldDropdown([
  486. [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
  487. [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
  488. [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
  489. [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
  490. [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
  491. [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
  492. [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
  493. [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
  494. [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
  495. ]), "BCOLOR");
  496. this.appendValueInput("CONTENT")
  497. .setCheck("String")
  498. .appendField(Blockly.Msg.image_process_text_content);
  499. /* this.appendDummyInput()
  500. .appendField(Blockly.Msg.LCDDT); */
  501. this.appendValueInput("POSA")
  502. .setCheck("String")
  503. .appendField(Blockly.Msg.image_process_text_start_coord);
  504. this.setPreviousStatement(true, null);
  505. this.setNextStatement(true, null);
  506. this.setColour("#5bb2d6");
  507. this.setTooltip('');
  508. this.setHelpUrl('');
  509. }
  510. };
  511. Blockly.Python['ai_lcd_colortext'] = function (block) {
  512. var color = block.getFieldValue("COLOR");
  513. var bcolor = block.getFieldValue("BCOLOR");
  514. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  515. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  516. //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  517. var _code = "lcd.draw_string(" + xy + ", " + content + ", lcd." + color + ", lcd." + bcolor + ")\n"; //, scale=" + size + "
  518. // return [_code, Blockly.Python.ORDER_ATOMIC]
  519. return _code;
  520. };
  521. Blockly.Blocks["ai_lcd_createnonecarvas"] = {
  522. init: function () {
  523. this.appendDummyInput()
  524. .appendField(Blockly.Msg.image_process_create_blank_canvas)
  525. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  526. this.appendValueInput("wh")
  527. .setCheck(null)
  528. .appendField(Blockly.Msg.image_process_text_size);
  529. // this.appendDummyInput()
  530. // .appendField(Blockly.Msg.LCDDCC_F)
  531. // .appendField(new Blockly.FieldCheckbox("TRUE"), "check")
  532. // .appendField(Blockly.Msg.LCDDCC_I)
  533. // .appendField(new Blockly.FieldTextInput(""), "text");
  534. this.setInputsInline(true);
  535. this.setPreviousStatement(true, null);
  536. this.setNextStatement(true, null);
  537. this.setColour("#5cb2d6");
  538. this.setTooltip(Blockly.Msg.ai_lcd_createnonecarvas_TOOLTIP);
  539. this.setHelpUrl('');
  540. }
  541. }
  542. Blockly.Python.ai_lcd_createnonecarvas = function (block) {
  543. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  544. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  545. var _code = variable_name + " = image.Image(size=(" + value_wh + "))\n";
  546. return _code;
  547. }
  548. Blockly.Blocks["ai_lcd_fillcanvas"] = {
  549. init: function () {
  550. this.appendDummyInput()
  551. .appendField(Blockly.Msg.image_process_text_let_canvas)
  552. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  553. this.appendValueInput("color")
  554. .setCheck(null)
  555. .appendField(Blockly.Msg.image_process_text_let_canvas_filled_with_color);
  556. this.setInputsInline(true);
  557. this.setPreviousStatement(true, null);
  558. this.setNextStatement(true, null);
  559. this.setColour("#5cb2d6");
  560. this.setTooltip(Blockly.Msg.ai_lcd_fillcanvas_TOOLTIP);
  561. this.setHelpUrl('');
  562. }
  563. }
  564. Blockly.Python.ai_lcd_fillcanvas = function (block) {
  565. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  566. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  567. var _code = variable_name + '.draw_rectangle(0,0, ' + variable_name + '.width(),' + variable_name + '.height(), color=' + value_color + ', thickness=1, fill=True)\n';
  568. return _code;
  569. }
  570. Blockly.Blocks["ai_lcd_createcarvas"] = {
  571. init: function () {
  572. this.appendDummyInput()
  573. .appendField(Blockly.Msg.image_process_create_image_canvas)
  574. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  575. this.appendDummyInput()
  576. .appendField(Blockly.Msg.image_process_create_image_canvas_path)
  577. .appendField(new Blockly.FieldTextInput("/sd/preset/images/splash_bg.jpg"), "text");
  578. this.setPreviousStatement(true, null);
  579. this.setNextStatement(true, null);
  580. this.setColour("#5cb2d6");
  581. this.setTooltip(Blockly.Msg.ai_lcd_createcarvas_TOOLTIP);
  582. this.setHelpUrl('');
  583. }
  584. }
  585. Blockly.Python.ai_lcd_createcarvas = function (block) {
  586. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  587. var checkbox_name = block.getFieldValue('check') == 'TRUE' ? "True" : "False";
  588. var text_name = block.getFieldValue('text');
  589. var _code = variable_name + " = image.Image(\"" + text_name + "\")\n";
  590. return _code;
  591. }
  592. Blockly.Blocks['ai_lcd_showcarvas_set_cord'] = {
  593. init: function () {
  594. this.appendDummyInput()
  595. .appendField(Blockly.Msg.image_process_text_set_canvas_setup)
  596. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  597. this.appendValueInput("xy")
  598. .setCheck(null)
  599. .appendField(Blockly.Msg.image_process_text_start_coord_as);
  600. this.setInputsInline(true);
  601. this.setPreviousStatement(true, null);
  602. this.setNextStatement(true, null);
  603. this.setColour("#5cb2d6");
  604. this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_cord_TOOLTIP);
  605. this.setHelpUrl("");
  606. }
  607. };
  608. Blockly.Python.ai_lcd_showcarvas_set_cord = function (block) {
  609. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  610. var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  611. // TODO: Assemble Python into code variable.
  612. var code = "_" + variable_name + "_x, _" + variable_name + "_y = " + value_name + "\n";
  613. return code;
  614. };
  615. Blockly.Blocks['ai_lcd_showcarvas_set_display'] = {
  616. init: function () {
  617. this.appendDummyInput()
  618. .appendField(Blockly.Msg.image_process_show_canvas)
  619. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  620. this.setInputsInline(false);
  621. this.setPreviousStatement(true, null);
  622. this.setNextStatement(true, null);
  623. this.setColour("#5cb2d6");
  624. this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_display_TOOLTIP);
  625. this.setHelpUrl("");
  626. }
  627. };
  628. Blockly.Python.ai_lcd_showcarvas_set_display = function (block) {
  629. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  630. Blockly.Python.definitions_.ai_lcd_display_def = "" +
  631. "_" + variable_name + "_x, _" + variable_name + "_y = 0, 0" +
  632. "";
  633. // TODO: Assemble Python into code variable.
  634. var code = "lcd.display(" + variable_name + ", oft=(_" + variable_name + "_x,_" + variable_name + "_y))\n";
  635. return code;
  636. };
  637. Blockly.Blocks['ai_lcd_showcarvas'] = {
  638. init: function () {
  639. this.appendDummyInput()
  640. .appendField(Blockly.Msg.image_process_show_canvas)
  641. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  642. this.appendValueInput("xy")
  643. .setCheck(null)
  644. .appendField(Blockly.Msg.image_process_text_start_coord);
  645. this.setInputsInline(false);
  646. this.setPreviousStatement(true, null);
  647. this.setNextStatement(true, null);
  648. this.setColour("#5cb2d6");
  649. this.setTooltip("");
  650. this.setHelpUrl("");
  651. }
  652. };
  653. Blockly.Python.ai_lcd_showcarvas = function (block) {
  654. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  655. var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  656. // TODO: Assemble Python into code variable.
  657. var code = "lcd.display(" + variable_name + ", oft=(" + value_name + "))\n";
  658. return code;
  659. };
  660. Blockly.Blocks['ai_lcd_clearcanvas'] = {
  661. init: function () {
  662. this.appendDummyInput()
  663. .appendField(Blockly.Msg.image_process_clear_canvas_1)
  664. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  665. .appendField(Blockly.Msg.image_process_clear_canvas_2);
  666. this.setPreviousStatement(true, null);
  667. this.setNextStatement(true, null);
  668. this.setColour("#5bb2d6");
  669. this.setTooltip(Blockly.Msg.ai_lcd_clearcanvas_TOOLTIP);
  670. this.setHelpUrl('');
  671. }
  672. }
  673. Blockly.Python.ai_lcd_clearcanvas = function (block) {
  674. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  675. var _code = variable_name + ".clear()\n";
  676. return _code;;
  677. }
  678. Blockly.Blocks['ai_lcd_textcarvas'] = {
  679. init: function () {
  680. this.appendDummyInput()
  681. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  682. this.appendDummyInput()
  683. .appendField(Blockly.Msg.image_process_text_on_canvas)
  684. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  685. .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_text);
  686. this.appendValueInput("POSA")
  687. .setCheck("String")
  688. .appendField(Blockly.Msg.image_process_text_start_coord);
  689. this.appendValueInput("CONTENT")
  690. .setCheck("String")
  691. .appendField(Blockly.Msg.image_process_text_content);
  692. this.appendValueInput("COLOR")
  693. .setCheck("String")
  694. .appendField(Blockly.Msg.image_process_text_color);
  695. this.appendValueInput("Scale")
  696. .setCheck("Number")
  697. .appendField(Blockly.Msg.image_process_draw_text_font_size);
  698. this.appendDummyInput()
  699. .appendField(Blockly.Msg.image_process_draw_text_if_monospace)
  700. .appendField(new Blockly.FieldCheckbox("FALSE"), "bool");
  701. this.setInputsInline(false);
  702. this.setPreviousStatement(true);
  703. this.setNextStatement(true);
  704. this.setColour("#5bb2d6");
  705. this.setTooltip(Blockly.Msg.ai_lcd_textcarvas_TOOLTIP);
  706. this.setHelpUrl('');
  707. }
  708. };
  709. Blockly.Python.ai_lcd_textcarvas = function (block) {
  710. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  711. if (color.charAt(0) == '#') {
  712. var d = 0,
  713. e = 0,
  714. f = 0;
  715. try {
  716. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  717. e = parseInt(color.substring(3, 5), 16),
  718. f = parseInt(color.substring(5, 7), 16))
  719. } catch (g) { }
  720. var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
  721. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  722. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  723. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  724. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  725. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  726. var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=(" + d + "," + e + "," + f + "), scale=" + size + ", mono_space=" + bool + ")\n";
  727. } else if (color.charAt(0) != '#') {
  728. var d = 0,
  729. e = 0,
  730. f = 0;
  731. try {
  732. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  733. e = parseInt(color.substring(3, 5), 16),
  734. f = parseInt(color.substring(5, 7), 16))
  735. } catch (g) { }
  736. var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
  737. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  738. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  739. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  740. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  741. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  742. var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=" + color + ", scale=" + size + ", mono_space=" + bool + ")\n";
  743. }
  744. return _code;
  745. }
  746. Blockly.Blocks['ai_lcd_characterscarvas'] = {
  747. init: function () {
  748. this.appendDummyInput()
  749. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_hanzi_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  750. this.appendDummyInput()
  751. .appendField(Blockly.Msg.image_process_text_on_canvas)
  752. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  753. .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_characters);
  754. this.appendValueInput("POSA")
  755. .setCheck("String")
  756. .appendField(Blockly.Msg.image_process_text_start_coord);
  757. this.appendValueInput("CONTENT")
  758. .setCheck("String")
  759. .appendField(Blockly.Msg.image_process_text_content);
  760. this.appendValueInput("COLOR")
  761. .setCheck("String")
  762. .appendField(Blockly.Msg.image_process_text_color);
  763. this.appendDummyInput()
  764. .appendField(Blockly.Msg.image_process_draw_text_font_size + ":")
  765. // .setCheck(null)
  766. .appendField(new Blockly.FieldDropdown([
  767. [Blockly.Msg.SMALL, "1"],
  768. [Blockly.Msg.MIDDLE, "2"],
  769. [Blockly.Msg.LARGE, "3"]
  770. ]), "fonts");
  771. // this.appendDummyInput()
  772. // .appendField(Blockly.Msg.Set_Auto_Wrap + ":")
  773. // // .setCheck(null)
  774. // .appendField(new Blockly.FieldDropdown([
  775. // [Blockly.Msg.LED_SCROLL_YES, "True"],
  776. // [Blockly.Msg.LED_SCROLL_NO, "False"]
  777. // ]), "wraps");
  778. this.setInputsInline(false);
  779. this.setPreviousStatement(true);
  780. this.setNextStatement(true);
  781. this.setColour("#5bb2d6");
  782. this.setTooltip(Blockly.Msg.ai_lcd_characterscarvas_TOOLTIP);
  783. this.setHelpUrl('');
  784. }
  785. };
  786. Blockly.Python.ai_lcd_characterscarvas = function (block) {
  787. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  788. if (color.charAt(0) == '#') {
  789. var d = 0,
  790. e = 0,
  791. f = 0;
  792. try {
  793. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  794. e = parseInt(color.substring(3, 5), 16),
  795. f = parseInt(color.substring(5, 7), 16))
  796. } catch (g) { }
  797. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  798. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  799. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  800. var size = block.getFieldValue('fonts');;
  801. // var wraps = block.getFieldValue('wraps');
  802. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  803. var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=(" + d + "," + e + "," + f + "))\n";
  804. } else if (color.charAt(0) != '#') {
  805. var d = 0,
  806. e = 0,
  807. f = 0;
  808. try {
  809. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  810. e = parseInt(color.substring(3, 5), 16),
  811. f = parseInt(color.substring(5, 7), 16))
  812. } catch (g) { }
  813. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  814. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  815. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  816. var size = block.getFieldValue('fonts');;
  817. // var wraps = block.getFieldValue('wraps');
  818. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  819. var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=" + color + ")\n";
  820. }
  821. return _code;
  822. }
  823. Blockly.Blocks['ai_lcd_linecarvas'] = {
  824. init: function () {
  825. this.appendDummyInput()
  826. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  827. this.appendDummyInput()
  828. .appendField(Blockly.Msg.image_process_text_on_canvas)
  829. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  830. .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_line);
  831. this.appendValueInput("POSA")
  832. .setCheck("String")
  833. .appendField(Blockly.Msg.image_process_text_start_coord);
  834. this.appendValueInput("POSB")
  835. .setCheck("String")
  836. .appendField(Blockly.Msg.image_process_text_end_coord);
  837. this.appendValueInput("COLOR")
  838. .setCheck("String")
  839. .appendField(Blockly.Msg.image_process_text_color);
  840. this.appendValueInput("Scale")
  841. .setCheck("Number")
  842. .appendField(Blockly.Msg.image_process_text_thick);
  843. this.setInputsInline(false);
  844. this.setPreviousStatement(true);
  845. this.setNextStatement(true);
  846. this.setColour("#5bb2d6");
  847. this.setTooltip(Blockly.Msg.ai_lcd_linecarvas_TOOLTIP);
  848. this.setHelpUrl('');
  849. }
  850. };
  851. Blockly.Python.ai_lcd_linecarvas = function (block) {
  852. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  853. if (color.charAt(0) == '#') {
  854. var d = 0,
  855. e = 0,
  856. f = 0;
  857. try {
  858. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  859. e = parseInt(color.substring(3, 5), 16),
  860. f = parseInt(color.substring(5, 7), 16))
  861. } catch (g) { }
  862. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  863. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  864. var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  865. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  866. var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=(" + d + "," + e + "," + f + "), thickness=" + size + ")\n";
  867. } else if (color.charAt(0) != '#') {
  868. var d = 0,
  869. e = 0,
  870. f = 0;
  871. try {
  872. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  873. e = parseInt(color.substring(3, 5), 16),
  874. f = parseInt(color.substring(5, 7), 16))
  875. } catch (g) { }
  876. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  877. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  878. var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  879. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  880. var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=" + color + ", thickness=" + size + ")\n";
  881. }
  882. return _code;
  883. }
  884. Blockly.Blocks['ai_lcd_ractanglecarvas'] = {
  885. init: function () {
  886. // this.appendDummyInput()
  887. // .appendField(
  888. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  889. this.appendDummyInput()
  890. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  891. this.appendDummyInput()
  892. .appendField(Blockly.Msg.image_process_text_on_canvas)
  893. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  894. .appendField(Blockly.Msg.image_process_text_on_draw)
  895. .appendField(new Blockly.FieldDropdown([
  896. [Blockly.Msg.image_process_text_filled, "fillrect"],
  897. [Blockly.Msg.image_process_text_stroked, "rect"]
  898. ]), "rect_type")
  899. .appendField(Blockly.Msg.image_process_draw_rectangle_text);
  900. this.appendValueInput("POSA")
  901. .setCheck("String")
  902. .appendField(Blockly.Msg.image_process_text_start_coord);
  903. this.appendValueInput("POSB")
  904. .setCheck("String")
  905. .appendField(Blockly.Msg.image_process_text_size);
  906. this.appendValueInput("COLOR")
  907. .setCheck("String")
  908. .appendField(Blockly.Msg.image_process_text_color);
  909. this.appendValueInput("Scale")
  910. .setCheck("Number")
  911. .appendField(Blockly.Msg.image_process_text_thick);
  912. this.setInputsInline(false);
  913. this.setPreviousStatement(true);
  914. this.setNextStatement(true);
  915. this.setColour("#5bb2d6");
  916. var thisBlock = this;
  917. this.setTooltip(function () {
  918. var mode = thisBlock.getFieldValue('rect_type');
  919. var TOOLTIPS = {
  920. 'fillrect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
  921. 'rect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
  922. };
  923. return TOOLTIPS[mode];
  924. });
  925. this.setHelpUrl('');
  926. }
  927. };
  928. Blockly.Python.ai_lcd_ractanglecarvas = function (block) {
  929. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  930. if (color.charAt(0) == '#') {
  931. var d = 0,
  932. e = 0,
  933. f = 0;
  934. try {
  935. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  936. e = parseInt(color.substring(3, 5), 16),
  937. f = parseInt(color.substring(5, 7), 16))
  938. } catch (g) { }
  939. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  940. var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
  941. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  942. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  943. var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  944. var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=(" + d + "," + e + "," + f + "), thickness=" + th + ", fill=" + type + ")\n";
  945. } else if (color.charAt(0) != '#') {
  946. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  947. var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
  948. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  949. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  950. var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  951. var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=" + color + ", thickness=" + th + ", fill=" + type + ")\n";
  952. }
  953. return _code;
  954. }
  955. Blockly.Blocks['ai_lcd_drawcirclecarvas'] = {
  956. init: function () {
  957. this.appendDummyInput()
  958. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  959. // this.appendDummyInput()
  960. // .appendField(
  961. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  962. this.appendDummyInput()
  963. .appendField(Blockly.Msg.image_process_text_on_canvas)
  964. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  965. .appendField(Blockly.Msg.image_process_text_on_draw)
  966. .appendField(new Blockly.FieldDropdown([
  967. [Blockly.Msg.image_process_text_filled, "fillrect"],
  968. [Blockly.Msg.image_process_text_stroked, "rect"]
  969. ]), "rect_type")
  970. .appendField(Blockly.Msg.image_process_draw_circle_text);
  971. this.appendValueInput("POSA")
  972. .setCheck("String")
  973. .appendField(Blockly.Msg.image_process_draw_circle_text_start);
  974. this.appendValueInput("Scale")
  975. .setCheck("Number")
  976. .appendField(Blockly.Msg.image_process_text_radius);
  977. this.appendValueInput("COLOR")
  978. .setCheck("String")
  979. .appendField(Blockly.Msg.image_process_text_color);
  980. this.appendValueInput("Scale_0")
  981. .setCheck("Number")
  982. .appendField(Blockly.Msg.image_process_text_thick);
  983. this.setInputsInline(false);
  984. this.setPreviousStatement(true);
  985. this.setNextStatement(true);
  986. this.setColour("#5bb2d6");
  987. var thisBlock = this;
  988. this.setTooltip(function () {
  989. var mode = thisBlock.getFieldValue('rect_type');
  990. var TOOLTIPS = {
  991. 'fillrect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
  992. 'rect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
  993. };
  994. return TOOLTIPS[mode];
  995. });
  996. this.setHelpUrl('');
  997. }
  998. };
  999. Blockly.Python.ai_lcd_drawcirclecarvas = function (block) {
  1000. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1001. if (color.charAt(0) == '#') {
  1002. var d = 0,
  1003. e = 0,
  1004. f = 0;
  1005. try {
  1006. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1007. e = parseInt(color.substring(3, 5), 16),
  1008. f = parseInt(color.substring(5, 7), 16))
  1009. } catch (g) { }
  1010. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1011. var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
  1012. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1013. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1014. var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
  1015. var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=(" + d + ", " + e + ", " + f + "), thickness=" + th0 + ", fill=" + type + ")\n";
  1016. } else if (color.charAt(0) != '#') {
  1017. var d = 0,
  1018. e = 0,
  1019. f = 0;
  1020. try {
  1021. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1022. e = parseInt(color.substring(3, 5), 16),
  1023. f = parseInt(color.substring(5, 7), 16))
  1024. } catch (g) { }
  1025. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1026. var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
  1027. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1028. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1029. var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
  1030. var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=" + color + ", thickness=" + th0 + ", fill=" + type + ")\n";
  1031. }
  1032. return _code;
  1033. }
  1034. Blockly.Blocks['ai_lcd_draw_cross_sign'] = {
  1035. init: function () {
  1036. this.appendDummyInput()
  1037. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_cross.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1038. this.appendDummyInput()
  1039. .appendField(Blockly.Msg.image_process_text_on_canvas)
  1040. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1041. .appendField(Blockly.Msg.image_process_draw_cross_title);
  1042. this.appendValueInput("xy")
  1043. .setCheck(null)
  1044. .appendField(Blockly.Msg.image_process_draw_cross_centerpos);
  1045. this.appendValueInput("size")
  1046. .setCheck(null)
  1047. .appendField(Blockly.Msg.image_process_draw_cross_length);
  1048. this.appendValueInput("thickness")
  1049. .setCheck(null)
  1050. .appendField(Blockly.Msg.image_process_draw_cross_thick);
  1051. this.appendValueInput("color")
  1052. .setCheck(null)
  1053. .appendField(Blockly.Msg.image_process_draw_cross_color);
  1054. this.setInputsInline(false);
  1055. this.setPreviousStatement(true, null);
  1056. this.setNextStatement(true, null);
  1057. this.setColour("#5bb2d6");
  1058. this.setTooltip(Blockly.Msg.ai_lcd_draw_cross_sign_TOOLTIP);
  1059. this.setHelpUrl("");
  1060. }
  1061. };
  1062. Blockly.Python['ai_lcd_draw_cross_sign'] = function (block) {
  1063. var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1064. if (color.charAt(0) == '#') {
  1065. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1066. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1067. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1068. var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
  1069. var d = 0,
  1070. e = 0,
  1071. f = 0;
  1072. try {
  1073. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1074. e = parseInt(color.substring(3, 5), 16),
  1075. f = parseInt(color.substring(5, 7), 16))
  1076. } catch (g) { }
  1077. // TODO: Assemble Python into code variable.
  1078. var code = variable_name + '.draw_cross(' + value_xy + ', color=(' + d + ', ' + e + ', ' + f + '), size=' + value_size + ', thickness=' + value_thickness + ')\n';
  1079. } else if (color.charAt(0) != '#') {
  1080. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1081. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1082. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1083. var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
  1084. // TODO: Assemble Python into code variable.
  1085. var code = variable_name + '.draw_cross(' + value_xy + ', color=' + color + ', size=' + value_size + ', thickness=' + value_thickness + ')\n';
  1086. }
  1087. return code;
  1088. };
  1089. Blockly.Blocks['ai_lcd_draw_arrow_sign'] = {
  1090. init: function () {
  1091. this.appendDummyInput()
  1092. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_arrow.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1093. this.appendDummyInput()
  1094. .appendField(Blockly.Msg.image_process_text_on_canvas)
  1095. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1096. .appendField(Blockly.Msg.image_process_draw_arrow_title);
  1097. this.appendValueInput("xy1")
  1098. .setCheck(null)
  1099. .appendField(Blockly.Msg.image_process_draw_arrow_start);
  1100. this.appendValueInput("xy2")
  1101. .setCheck(null)
  1102. .appendField(Blockly.Msg.image_process_draw_arrow_end);
  1103. this.appendValueInput("color")
  1104. .setCheck(null)
  1105. .appendField(Blockly.Msg.image_process_draw_arrow_color);
  1106. this.appendValueInput("thickness")
  1107. .setCheck(null)
  1108. .appendField(Blockly.Msg.image_process_draw_arrow_thick);
  1109. this.setInputsInline(false);
  1110. this.setPreviousStatement(true, null);
  1111. this.setNextStatement(true, null);
  1112. this.setColour("#5bb2d6");
  1113. this.setTooltip(Blockly.Msg.ai_lcd_draw_arrow_sign_TOOLTIP);
  1114. this.setHelpUrl("");
  1115. }
  1116. };
  1117. Blockly.Python['ai_lcd_draw_arrow_sign'] = function (block) {
  1118. // TODO: Assemble Python into code variable.
  1119. var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1120. if (color.charAt(0) == '#') {
  1121. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1122. var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
  1123. var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
  1124. var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
  1125. var d = 0,
  1126. e = 0,
  1127. f = 0;
  1128. try {
  1129. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1130. e = parseInt(color.substring(3, 5), 16),
  1131. f = parseInt(color.substring(5, 7), 16))
  1132. } catch (g) { }
  1133. // TODO: Assemble Python into code variable.
  1134. var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + d + ', ' + e + ', ' + f + '), thickness=' + value_thickness + ')\n';
  1135. } else if (color.charAt(0) != '#') {
  1136. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1137. var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
  1138. var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
  1139. var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
  1140. // TODO: Assemble Python into code variable.
  1141. var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + color + '), thickness=' + value_thickness + ')\n';
  1142. }
  1143. return code;
  1144. };
  1145. Blockly.Blocks['ai_lcd_draw_image_on_canvas'] = {
  1146. init: function () {
  1147. this.appendDummyInput()
  1148. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_image.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1149. this.appendDummyInput()
  1150. .appendField(Blockly.Msg.image_process_text_on_canvas)
  1151. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1152. .appendField(Blockly.Msg.image_process_draw_image_title);
  1153. this.appendValueInput("image_path")
  1154. .setCheck(null)
  1155. .appendField(Blockly.Msg.image_process_draw_image_title_image_path);
  1156. this.appendValueInput("xy")
  1157. .setCheck(null)
  1158. .appendField(Blockly.Msg.image_process_text_start_coord);
  1159. this.appendValueInput("scale_x")
  1160. .setCheck(null)
  1161. .appendField(Blockly.Msg.image_process_draw_image_scale_x);
  1162. this.appendValueInput("scale_y")
  1163. .setCheck(null)
  1164. .appendField(Blockly.Msg.image_process_draw_image_scale_y);
  1165. this.setInputsInline(false);
  1166. this.setPreviousStatement(true, null);
  1167. this.setNextStatement(true, null);
  1168. this.setColour("#5bb2d6");
  1169. this.setTooltip(Blockly.Msg.ai_lcd_draw_image_on_canvas_TOOLTIP);
  1170. this.setHelpUrl("");
  1171. }
  1172. };
  1173. Blockly.Python['ai_lcd_draw_image_on_canvas'] = function (block) {
  1174. // TODO: Assemble Python into code variable.
  1175. // nav_up = image.Image("/sd/preset/images/arrow_up_filled.jpg")
  1176. // splash.draw_image(nav_up, 5, 5)
  1177. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1178. var value_image_path = Blockly.Python.valueToCode(block, 'image_path', Blockly.Python.ORDER_ATOMIC);
  1179. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1180. var value_scale_x = Blockly.Python.valueToCode(block, 'scale_x', Blockly.Python.ORDER_ATOMIC);
  1181. var value_scale_y = Blockly.Python.valueToCode(block, 'scale_y', Blockly.Python.ORDER_ATOMIC);
  1182. // TODO: Assemble Python into code variable.
  1183. var code = '' +
  1184. variable_name + '.draw_image(image.Image(' + value_image_path + '), ' + value_xy + ', x_scale=' + value_scale_x + ', y_scale=' + value_scale_y + ' )' +
  1185. '\n';
  1186. return code;
  1187. };
  1188. Blockly.Blocks['ai_img_save'] = {
  1189. init: function () {
  1190. this.appendValueInput("save_path")
  1191. .setCheck(null)
  1192. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1193. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1194. .appendField(Blockly.Msg.image_process_img_save);
  1195. this.appendDummyInput()
  1196. .appendField(Blockly.Msg.image_process_img_save_quality)
  1197. .appendField(new Blockly.FieldNumber(90, 0, 100), "compression")
  1198. .appendField("%");
  1199. this.setInputsInline(false);
  1200. this.setPreviousStatement(true, null);
  1201. this.setNextStatement(true, null);
  1202. this.setColour("#f0983e");
  1203. this.setTooltip(Blockly.Msg.ai_img_save_TOOLTIP);
  1204. this.setHelpUrl("");
  1205. }
  1206. };
  1207. Blockly.Python['ai_img_save'] = function (block) {
  1208. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1209. var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  1210. var compression_rate = block.getFieldValue('compression');
  1211. // TODO: Assemble Python into code variable.
  1212. var code = variable_name + '.save(' + save_path_location + ', quality=' + compression_rate + ')\n';
  1213. return code;
  1214. };
  1215. Blockly.Blocks['ai_lcd_invert_color'] = {
  1216. init: function () {
  1217. this.appendDummyInput()
  1218. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1219. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1220. .appendField(Blockly.Msg.image_process_color_invert);
  1221. this.setOutput(true, null);
  1222. this.setColour("#f0983e");
  1223. this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
  1224. this.setHelpUrl("");
  1225. }
  1226. };
  1227. Blockly.Python['ai_lcd_invert_color'] = function (block) {
  1228. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1229. // TODO: Assemble Python into code variable.
  1230. var code = variable_name + '.invert()';
  1231. // TODO: Change ORDER_NONE to the correct strength.
  1232. return [code, Blockly.Python.ORDER_NONE];
  1233. };
  1234. Blockly.Blocks['ai_lcd_resizecanvas'] = {
  1235. init: function () {
  1236. this.appendDummyInput()
  1237. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1238. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1239. .appendField(Blockly.Msg.image_process_resize_to);
  1240. this.appendValueInput("wh")
  1241. .setCheck(null);
  1242. this.setInputsInline(true);
  1243. this.setOutput(true, null);
  1244. this.setColour("#f0983e");
  1245. this.setTooltip(Blockly.Msg.ai_lcd_resizecanvas_TOOLTIP);
  1246. this.setHelpUrl("");
  1247. }
  1248. };
  1249. Blockly.Python['ai_lcd_resizecanvas'] = function (block) {
  1250. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1251. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  1252. // TODO: Assemble Python into code variable.
  1253. var code = variable_name + '.resize(' + wh + ')';
  1254. // TODO: Change ORDER_NONE to the correct strength.
  1255. return [code, Blockly.Python.ORDER_NONE];
  1256. };
  1257. Blockly.Blocks['ai_lcd_color_scheme'] = {
  1258. init: function () {
  1259. this.appendDummyInput()
  1260. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1261. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1262. .appendField(Blockly.Msg.image_process_color_scheme_convert)
  1263. .appendField(new Blockly.FieldDropdown([
  1264. [Blockly.Msg.image_process_color_scheme_bw, "to_grayscale(1)"],
  1265. [Blockly.Msg.image_process_color_scheme_rgb, "to_rgb565(1)"]
  1266. ]), "color_scheme");
  1267. this.setOutput(true, null);
  1268. this.setColour("#f0983e");
  1269. var thisBlock = this;
  1270. this.setTooltip(function () {
  1271. var mode = thisBlock.getFieldValue('color_scheme');
  1272. var TOOLTIPS = {
  1273. 'to_grayscale(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_bw),
  1274. 'to_rgb565(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_rgb)
  1275. };
  1276. return TOOLTIPS[mode];
  1277. });
  1278. this.setHelpUrl("");
  1279. }
  1280. };
  1281. Blockly.Python['ai_lcd_color_scheme'] = function (block) {
  1282. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1283. var dropdown_vari = block.getFieldValue('color_scheme');
  1284. // TODO: Assemble Python into code variable.
  1285. var code = variable_name + '.' + dropdown_vari;
  1286. // TODO: Change ORDER_NONE to the correct strength.
  1287. return [code, Blockly.Python.ORDER_NONE];
  1288. };
  1289. Blockly.Blocks['ai_lcd_ai_pix'] = {
  1290. init: function () {
  1291. this.appendDummyInput()
  1292. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1293. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1294. .appendField(Blockly.Msg.image_process_pixtovec_text_first)
  1295. .appendField(new Blockly.FieldDropdown([
  1296. [Blockly.Msg.image_process_pixtovec_v2p, ".ai_to_pix()"],
  1297. [Blockly.Msg.image_process_pixtovec_p2v, ".pix_to_ai()"]
  1298. ]), "ai_pix_type")
  1299. .appendField(Blockly.Msg.image_process_pixtovec_conversion);
  1300. this.setInputsInline(false);
  1301. this.setPreviousStatement(true, null);
  1302. this.setNextStatement(true, null);
  1303. this.setColour("#f0983e");
  1304. this.setTooltip("");
  1305. this.setHelpUrl("");
  1306. }
  1307. };
  1308. Blockly.Python['ai_lcd_ai_pix'] = function (block) {
  1309. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1310. var dropdown_vari = block.getFieldValue('ai_pix_type');
  1311. // TODO: Assemble Python into code variable.
  1312. var code = 'a = ' + variable_name + dropdown_vari + '\n';
  1313. // TODO: Change ORDER_NONE to the correct strength.
  1314. return code;
  1315. };
  1316. Blockly.Blocks['ai_lcd_img_cut'] = {
  1317. init: function () {
  1318. this.appendDummyInput()
  1319. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1320. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1321. .appendField(Blockly.Msg.image_process_imgcut_cutting);
  1322. this.appendValueInput("xy")
  1323. .setCheck(null)
  1324. .appendField(Blockly.Msg.image_process_imgcut_start_coord);
  1325. this.appendValueInput("wh")
  1326. .setCheck(null)
  1327. .appendField(Blockly.Msg.image_process_imgcut_size);
  1328. this.setInputsInline(false);
  1329. this.setOutput(true, null);
  1330. this.setColour("#f0983e");
  1331. this.setTooltip(Blockly.Msg.ai_lcd_img_cut_TOOLTIP);
  1332. this.setHelpUrl("");
  1333. }
  1334. };
  1335. Blockly.Python['ai_lcd_img_cut'] = function (block) {
  1336. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1337. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1338. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  1339. // TODO: Assemble Python into code variable.
  1340. // TODO: Change ORDER_NONE to the correct strength.
  1341. var code = variable_name + '.cut(' + xy + ',' + wh + ')';
  1342. return [code, Blockly.Python.ORDER_NONE];
  1343. };
  1344. Blockly.Blocks['ai_vision_get_statistics'] = {
  1345. init: function () {
  1346. this.appendDummyInput()
  1347. .appendField(Blockly.Msg.image_process_get_statistics_to_canvas)
  1348. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1349. .appendField(Blockly.Msg.image_process_get_statistics_get_analysis);
  1350. this.appendValueInput("xy")
  1351. .setCheck(null)
  1352. .appendField(Blockly.Msg.image_process_get_statistics_start_coord);
  1353. this.appendValueInput("wh")
  1354. .setCheck(null)
  1355. .appendField(Blockly.Msg.image_process_get_statistics_size);
  1356. this.appendValueInput("bins")
  1357. .setCheck(null)
  1358. .appendField(Blockly.Msg.image_process_get_statistics_boxes);
  1359. this.setInputsInline(false);
  1360. this.setOutput(true, null);
  1361. this.setColour("#f0983e");
  1362. this.setTooltip(Blockly.Msg.ai_vision_get_statistics_TOOLTIP);
  1363. this.setHelpUrl("");
  1364. }
  1365. };
  1366. Blockly.Python['ai_vision_get_statistics'] = function (block) {
  1367. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1368. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1369. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  1370. var bins = Blockly.Python.valueToCode(block, 'bins', Blockly.Python.ORDER_ATOMIC);
  1371. // TODO: Assemble Python into code variable.
  1372. var code = variable_name + '.get_statistics(bins=' + bins + ',roi=(' + xy + ',' + wh + '))';
  1373. // TODO: Change ORDER_NONE to the correct strength.
  1374. return [code, Blockly.Python.ORDER_NONE];
  1375. };
  1376. Blockly.Blocks['ai_vision_get_histogram'] = {
  1377. init: function () {
  1378. this.appendDummyInput()
  1379. .appendField(Blockly.Msg.ai_vision_get_histogram_get)
  1380. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1381. .appendField(Blockly.Msg.ai_vision_get_histogram_compute);
  1382. this.appendDummyInput()
  1383. .appendField(Blockly.Msg.ai_vision_get_histogram_region);
  1384. this.appendValueInput("xy")
  1385. .setCheck(null)
  1386. .appendField(Blockly.Msg.ai_vision_get_histogram_region_xy);
  1387. this.appendValueInput("wh")
  1388. .setCheck(null)
  1389. .appendField(Blockly.Msg.ai_vision_get_histogram_region_wh);
  1390. this.setInputsInline(false);
  1391. this.setOutput(true, null);
  1392. this.setColour("#f0983e");
  1393. this.setTooltip(Blockly.Msg.ai_vision_get_histogram_TOOLTIP);
  1394. this.setHelpUrl("");
  1395. }
  1396. };
  1397. Blockly.Python['ai_vision_get_histogram'] = function (block) {
  1398. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1399. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1400. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  1401. // TODO: Assemble Python into code variable.
  1402. var code = variable_name + '.get_histogram(roi=(' + value_xy + ',' + value_wh + '))';
  1403. // TODO: Change ORDER_NONE to the correct strength.
  1404. return [code, Blockly.Python.ORDER_NONE];
  1405. };
  1406. Blockly.Blocks['ai_vision_get_histogram_percentile'] = {
  1407. init: function () {
  1408. this.appendDummyInput()
  1409. .appendField(Blockly.Msg.ai_vision_get_histogram_details_1)
  1410. .appendField(new Blockly.FieldVariable("hist"), "varitem")
  1411. .appendField(Blockly.Msg.ai_vision_get_histogram_details_get);
  1412. this.appendValueInput("perc")
  1413. .setCheck(null);
  1414. this.setInputsInline(true);
  1415. this.setOutput(true, null);
  1416. this.setColour("#f0983e");
  1417. this.setTooltip(Blockly.Msg.ai_vision_get_histogram_percentile_TOOLTIP);
  1418. this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
  1419. }
  1420. };
  1421. Blockly.Python['ai_vision_get_histogram_percentile'] = function (block) {
  1422. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1423. var value_perc = Blockly.Python.valueToCode(block, 'perc', Blockly.Python.ORDER_ATOMIC);
  1424. // TODO: Assemble Python into code variable.
  1425. var code = variable_name + '.get_percentile(' + value_perc + ')';
  1426. // TODO: Change ORDER_NONE to the correct strength.
  1427. return [code, Blockly.Python.ORDER_NONE];
  1428. };
  1429. Blockly.Blocks['ai_vision_get_histogram_percentile_detail'] = {
  1430. init: function () {
  1431. this.appendDummyInput()
  1432. .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_1)
  1433. .appendField(new Blockly.FieldVariable("percentile"), "varitem");
  1434. this.appendDummyInput()
  1435. .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_2)
  1436. .appendField(new Blockly.FieldDropdown([
  1437. [Blockly.Msg.ai_vision_get_histogram_percentile_3, "value()"],
  1438. [Blockly.Msg.ai_vision_get_histogram_percentile_4, "l_value()"],
  1439. [Blockly.Msg.ai_vision_get_histogram_percentile_5, "a_value()"],
  1440. [Blockly.Msg.ai_vision_get_histogram_percentile_6, "b_value()"]
  1441. ]), "option");
  1442. this.setInputsInline(false);
  1443. this.setOutput(true, null);
  1444. this.setColour("#f0983e");
  1445. var thisBlock = this;
  1446. this.setTooltip(function () {
  1447. var mode = thisBlock.getFieldValue('option');
  1448. var TOOLTIPS = {
  1449. 'value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_3),
  1450. 'l_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_4),
  1451. 'a_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_5),
  1452. 'b_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_6)
  1453. };
  1454. return TOOLTIPS[mode];
  1455. });
  1456. this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
  1457. }
  1458. };
  1459. Blockly.Python['ai_vision_get_histogram_percentile_detail'] = function (block) {
  1460. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1461. var dropdown_option = block.getFieldValue('option');
  1462. var code = variable_name + '.' + dropdown_option;
  1463. // TODO: Change ORDER_NONE to the correct strength.
  1464. return [code, Blockly.Python.ORDER_NONE];
  1465. };
  1466. Blockly.Blocks['ai_vision_lab_rgb_conversion'] = {
  1467. init: function () {
  1468. this.appendDummyInput()
  1469. .appendField(Blockly.Msg.image_process_lab_rgb_conversion_exec)
  1470. .appendField(new Blockly.FieldDropdown([
  1471. [Blockly.Msg.image_process_lab_rgb_conversion_torgb, "lab_to_rgb"],
  1472. [Blockly.Msg.image_process_lab_rgb_conversion_tolab, "rgb_to_lab"]
  1473. ]), "rgblab");
  1474. this.appendValueInput("color_tuple")
  1475. .setCheck(null)
  1476. .appendField(Blockly.Msg.image_process_lab_rgb_conversion_tuple);
  1477. this.setOutput(true, null);
  1478. this.setColour("#f0983e");
  1479. var thisBlock = this;
  1480. this.setTooltip(function () {
  1481. var mode = thisBlock.getFieldValue('rgblab');
  1482. var TOOLTIPS = {
  1483. 'lab_to_rgb': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_torgb),
  1484. 'rgb_to_lab': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_tolab)
  1485. };
  1486. return TOOLTIPS[mode];
  1487. });
  1488. this.setHelpUrl("");
  1489. }
  1490. };
  1491. Blockly.Python['ai_vision_lab_rgb_conversion'] = function (block) {
  1492. var type = block.getFieldValue('rgblab');
  1493. var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
  1494. // TODO: Assemble Python into code variable.
  1495. var code = 'image.' + type + '(' + tuple + ')';
  1496. // TODO: Change ORDER_NONE to the correct strength.
  1497. return [code, Blockly.Python.ORDER_NONE];
  1498. };
  1499. Blockly.Blocks['ai_vision_rgb_grayscale_conversion'] = {
  1500. init: function () {
  1501. this.appendDummyInput()
  1502. .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_exec)
  1503. .appendField(new Blockly.FieldDropdown([
  1504. [Blockly.Msg.image_process_rgb_grayscale_conversion_torgb, "grayscale_to_rgb"],
  1505. [Blockly.Msg.image_process_rgb_grayscale_conversion_togray, "rgb_to_grayscale"]
  1506. ]), "rgb_g");
  1507. this.appendValueInput("color_tuple")
  1508. .setCheck(null)
  1509. .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_set);
  1510. this.setOutput(true, null);
  1511. this.setColour("#f0983e");
  1512. var thisBlock = this;
  1513. this.setTooltip(function () {
  1514. var mode = thisBlock.getFieldValue('rgb_g');
  1515. var TOOLTIPS = {
  1516. 'grayscale_to_rgb': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_torgb),
  1517. 'rgb_to_grayscale': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_togray)
  1518. };
  1519. return TOOLTIPS[mode];
  1520. });
  1521. this.setHelpUrl("");
  1522. }
  1523. };
  1524. Blockly.Python['ai_vision_rgb_grayscale_conversion'] = function (block) {
  1525. var type = block.getFieldValue('rgb_g');
  1526. var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
  1527. Blockly.Python.addVariable('rgb_tuple', "", true);
  1528. // TODO: Assemble Python into code variable.
  1529. var code = 'image.' + type + '(' + tuple + ')';
  1530. // TODO: Change ORDER_NONE to the correct strength.
  1531. return [code, Blockly.Python.ORDER_NONE];
  1532. };
  1533. /*
  1534. _ ___ ____
  1535. / \ |_ _| / ___|__ _ _ __ ___ ___ _ __ __ _
  1536. / _ \ | | | | / _` | '_ ` _ \ / _ \ '__/ _` |
  1537. / ___ \ | | | |__| (_| | | | | | | __/ | | (_| |
  1538. /_/ \_\___| \____\__,_|_| |_| |_|\___|_| \__,_|
  1539. */
  1540. Blockly.Blocks.ai_camera_wh = {
  1541. init: function () {
  1542. var _input = this.appendDummyInput();
  1543. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  1544. this.setColour("#60c1bb");
  1545. this.setOutput(!0, "Boolean");
  1546. _input.appendField(Blockly.Msg.W);
  1547. this.appendValueInput("A").setCheck("Number");
  1548. _input = this.appendDummyInput();
  1549. _input.appendField(Blockly.Msg.H)
  1550. this.appendValueInput("B").setCheck("Number");
  1551. this.setInputsInline(!0);
  1552. }
  1553. };
  1554. Blockly.Python.ai_camera_wh = function (block) {
  1555. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1556. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  1557. var code = "" + _x + ", " + _y + "";
  1558. return [code, Blockly.Python.ORDER_ATOMIC];
  1559. };
  1560. Blockly.Blocks['ai_camera_init'] = {
  1561. init: function () {
  1562. this.appendDummyInput()
  1563. .appendField(new Blockly.FieldImage("blockly/media/camera_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1564. this.appendDummyInput()
  1565. .appendField(Blockly.Msg.ai_camera_camera_init);
  1566. this.appendDummyInput()
  1567. .appendField(Blockly.Msg.ai_camera_camera_set_size)
  1568. .appendField(new Blockly.FieldDropdown([
  1569. ["QVGA (320*240)", "QVGA"],
  1570. ["QQVGA (160*120)", "QQVGA"]
  1571. ]), "DEGREE");
  1572. this.appendDummyInput()
  1573. .appendField(Blockly.Msg.ai_camera_camera_color_mode)
  1574. .appendField(new Blockly.FieldDropdown([
  1575. [Blockly.Msg.ai_camera_camera_color_mode_rgb, "RGB565"],
  1576. [Blockly.Msg.ai_camera_camera_color_mode_bw, "GRAYSCALE"]
  1577. ]), "DEGREE1");
  1578. this.setPreviousStatement(true, null);
  1579. this.setNextStatement(true, null);
  1580. this.setColour("#60c1bb");
  1581. this.setTooltip(Blockly.Msg.ai_camera_init_TOOLTIP);
  1582. this.setHelpUrl('');
  1583. }
  1584. };
  1585. Blockly.Python.ai_camera_init = function (block) {
  1586. var degee = block.getFieldValue('DEGREE');
  1587. var degee1 = block.getFieldValue('DEGREE1');
  1588. var checkbox = block.getFieldValue('checkbox') == 'TRUE';
  1589. var w = block.getFieldValue('w');
  1590. var h = block.getFieldValue('h');
  1591. var degee2 = block.getFieldValue('DEGREE2') || 1;
  1592. var degee3 = block.getFieldValue('DEGREE3') || 1;
  1593. var degee4 = block.getFieldValue('DEGREE4') || 1;
  1594. var degee5 = block.getFieldValue('DEGREE5') || 1;
  1595. var input1 = block.getFieldValue('input1') || 1;
  1596. var input2 = block.getFieldValue('input2') || 1;
  1597. var input3 = block.getFieldValue('input3') || 1;
  1598. Blockly.Python.definitions_.import_sensor = "import sensor";
  1599. var _code = "sensor.reset()\n";
  1600. _code += "sensor.set_pixformat(sensor." + degee1 + ")\n";
  1601. _code += "sensor.set_framesize(sensor." + degee + ")\n";
  1602. _code += "sensor.set_vflip(0)\n";
  1603. _code += "sensor.set_hmirror(0)\n";
  1604. _code += "sensor.skip_frames(30)\n";
  1605. _code += "sensor.run(1)\n";
  1606. return _code;
  1607. }
  1608. Blockly.Blocks['ai_camera_windows'] = {
  1609. init: function () {
  1610. this.appendDummyInput()
  1611. .appendField(Blockly.Msg.ai_camera_windowing_resize);
  1612. this.appendDummyInput()
  1613. .appendField(Blockly.Msg.ai_camera_windowing_w)
  1614. .appendField(new Blockly.FieldNumber(224), "window_w")
  1615. .appendField(Blockly.Msg.ai_camera_windowing_h)
  1616. .appendField(new Blockly.FieldNumber(224), "window_h")
  1617. .appendField(Blockly.Msg.ai_camera_windowing_unit);
  1618. this.setPreviousStatement(true, null);
  1619. this.setNextStatement(true, null);
  1620. this.setColour("#60c1bb");
  1621. this.setTooltip(Blockly.Msg.ai_camera_windows_TOOLTIP);
  1622. this.setHelpUrl("");
  1623. }
  1624. };
  1625. Blockly.Python['ai_camera_windows'] = function (block) {
  1626. var w = block.getFieldValue('window_w');
  1627. var h = block.getFieldValue('window_h');
  1628. // var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
  1629. var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
  1630. return code;
  1631. };
  1632. Blockly.Blocks['ai_camera_fit_to_screen_size'] = {
  1633. init: function () {
  1634. this.appendDummyInput()
  1635. .appendField(Blockly.Msg.ai_camera_camera_fit_set);
  1636. this.appendDummyInput()
  1637. .appendField(new Blockly.FieldVariable("camera"), "varitem");
  1638. this.appendDummyInput()
  1639. .appendField(Blockly.Msg.ai_camera_camera_fit_to)
  1640. .appendField(new Blockly.FieldDropdown([
  1641. [Blockly.Msg.ai_camera_camera_fit_1, "240"],
  1642. [Blockly.Msg.ai_camera_camera_fit_2, "224"]
  1643. ]), "size");
  1644. this.setInputsInline(true);
  1645. this.setPreviousStatement(true, null);
  1646. this.setNextStatement(true, null);
  1647. this.setColour("#60c1bb");
  1648. this.setTooltip(Blockly.Msg.ai_camera_fit_to_screen_size_TOOLTIP);
  1649. this.setHelpUrl("");
  1650. }
  1651. };
  1652. Blockly.Python['ai_camera_fit_to_screen_size'] = function (block) {
  1653. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1654. var dropdown_size = block.getFieldValue('size');
  1655. // TODO: Assemble Python into code variable.
  1656. if (dropdown_size == "240") {
  1657. var code = 'camera = sensor.snapshot()\n' +
  1658. variable_name + ' = ' + variable_name + '.cut(40,0,240,240)\n';
  1659. } else if (dropdown_size == "224") {
  1660. var code = 'camera = sensor.snapshot()\n' +
  1661. variable_name + ' = ' + variable_name + '.cut(48,8,224,224)\n' +
  1662. 'a = ' + variable_name + '.pix_to_ai()\n' +
  1663. '';
  1664. }
  1665. return code;
  1666. };
  1667. Blockly.Blocks['ai_camera_snapshot'] = {
  1668. init: function () {
  1669. this.appendDummyInput()
  1670. .appendField(Blockly.Msg.ai_camera_snapshot);
  1671. this.setOutput(true, null);
  1672. this.setColour("#60c1bb");
  1673. this.setTooltip(Blockly.Msg.ai_camera_snapshot_TOOLTIP);
  1674. this.setHelpUrl('');
  1675. }
  1676. };
  1677. Blockly.Python.ai_camera_snapshot = function (block) {
  1678. Blockly.Python.definitions_.import_sensor = "import sensor";
  1679. var degee = block.getFieldValue('DEGREE');
  1680. var _code = "sensor.snapshot()";
  1681. return [_code, Blockly.Python.ORDER_ATOMIC];
  1682. }
  1683. Blockly.Blocks['ai_camera_lens_corr'] = {
  1684. init: function () {
  1685. this.appendValueInput("lens_corr")
  1686. .setCheck(null)
  1687. .appendField(Blockly.Msg.ai_camera_set_lens_corr);
  1688. this.setInputsInline(true);
  1689. this.setOutput(true, null);
  1690. this.setColour("#60c1bb");
  1691. this.setTooltip(Blockly.Msg.ai_camera_lens_corr_TOOLTIP);
  1692. this.setHelpUrl("");
  1693. }
  1694. };
  1695. Blockly.Python['ai_camera_lens_corr'] = function (block) {
  1696. var value_lens_corr = Blockly.Python.valueToCode(block, 'lens_corr', Blockly.Python.ORDER_ATOMIC);
  1697. // TODO: Assemble Python into code variable.
  1698. var code = value_lens_corr + '.lens_corr(strength = 1.8, zoom = 1.0)';
  1699. // TODO: Change ORDER_NONE to the correct strength.
  1700. return [code, Blockly.Python.ORDER_NONE];
  1701. };
  1702. Blockly.Blocks['ai_camera_image_property_change'] = {
  1703. init: function () {
  1704. this.appendDummyInput()
  1705. .appendField(Blockly.Msg.ai_camera_bcs_set_text_2)
  1706. .appendField(new Blockly.FieldDropdown([
  1707. [Blockly.Msg.ai_camera_bcs_vflip, "set_vflip"],
  1708. [Blockly.Msg.ai_camera_bcs_hmirror, "set_hmirror"],
  1709. [Blockly.Msg.ai_camera_bcs_wb, "set_auto_whitebal"],
  1710. [Blockly.Msg.ai_camera_bcs_g, "set_auto_gain"],
  1711. [Blockly.Msg.ai_camera_bcs_e, "set_auto_exposure"]
  1712. ]), "image_property")
  1713. .appendField(Blockly.Msg.ai_camera_bcs_as_2)
  1714. .appendField(new Blockly.FieldDropdown([
  1715. [Blockly.Msg.ai_camera_enable + Blockly.Msg.ai_camera_bcs_status, "True"],
  1716. [Blockly.Msg.ai_camera_disable + Blockly.Msg.ai_camera_bcs_status, "False"]
  1717. ]), "DEGREE");
  1718. this.setPreviousStatement(true, null);
  1719. this.setNextStatement(true, null);
  1720. this.setColour("#60c1bb");
  1721. var thisBlock = this;
  1722. this.setTooltip(function () {
  1723. var mode = thisBlock.getFieldValue('image_property');
  1724. var TOOLTIPS = {
  1725. 'set_vflip': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_vflip),
  1726. 'set_hmirror': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_hmirror),
  1727. 'set_auto_whitebal': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_wb),
  1728. 'set_auto_gain': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_g),
  1729. 'set_auto_exposure': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_e)
  1730. };
  1731. return TOOLTIPS[mode];
  1732. });
  1733. this.setHelpUrl('');
  1734. }
  1735. };
  1736. Blockly.Python.ai_camera_image_property_change = function (block) {
  1737. var degee = block.getFieldValue('DEGREE');
  1738. var dropdown_image_property = block.getFieldValue('image_property');
  1739. var _code = "sensor." + dropdown_image_property + "(" + degee + ")\n";
  1740. return _code;
  1741. }
  1742. Blockly.Blocks['ai_camera_vflip'] = {
  1743. init: function () {
  1744. this.appendDummyInput()
  1745. .appendField(Blockly.Msg.ai_camera_set_vflip)
  1746. .appendField(new Blockly.FieldDropdown([
  1747. [Blockly.Msg.ai_camera_enable, "True"],
  1748. [Blockly.Msg.ai_camera_disable, "False"]
  1749. ]), "DEGREE")
  1750. this.setPreviousStatement(true, null);
  1751. this.setNextStatement(true, null);
  1752. this.setColour("#60c1bb");
  1753. this.setTooltip('');
  1754. this.setHelpUrl('');
  1755. }
  1756. };
  1757. Blockly.Python.ai_camera_vflip = function (block) {
  1758. var degee = block.getFieldValue('DEGREE');
  1759. var _code = "sensor.set_vflip(" + degee + ")\n";
  1760. return _code;
  1761. }
  1762. Blockly.Blocks['ai_camera_hmirror'] = {
  1763. init: function () {
  1764. this.appendDummyInput()
  1765. .appendField(Blockly.Msg.ai_camera_set_hmirror)
  1766. .appendField(new Blockly.FieldDropdown([
  1767. [Blockly.Msg.ai_camera_enable, "True"],
  1768. [Blockly.Msg.ai_camera_disable, "False"]
  1769. ]), "DEGREE")
  1770. this.setPreviousStatement(true, null);
  1771. this.setNextStatement(true, null);
  1772. this.setColour("#60c1bb");
  1773. this.setTooltip('');
  1774. this.setHelpUrl('');
  1775. }
  1776. };
  1777. Blockly.Python.ai_camera_hmirror = function (block) {
  1778. var degee = block.getFieldValue('DEGREE');
  1779. var _code = "sensor.set_hmirror(" + degee + ")\n"
  1780. return _code;
  1781. }
  1782. Blockly.Blocks['ai_camera_auto_whitebal'] = {
  1783. init: function () {
  1784. this.appendDummyInput()
  1785. .appendField(Blockly.Msg.ai_camera_set_auto_whitebal)
  1786. .appendField(new Blockly.FieldDropdown([
  1787. [Blockly.Msg.ai_camera_enable, "True"],
  1788. [Blockly.Msg.ai_camera_disable, "False"]
  1789. ]), "DEGREE")
  1790. this.setPreviousStatement(true, null);
  1791. this.setNextStatement(true, null);
  1792. this.setColour("#60c1bb");
  1793. this.setTooltip('');
  1794. this.setHelpUrl('');
  1795. }
  1796. };
  1797. Blockly.Python.ai_camera_auto_whitebal = function (block) {
  1798. var degee = block.getFieldValue('DEGREE');
  1799. var _code = "sensor.set_auto_whitebal(" + degee + ")\n"
  1800. return _code;
  1801. }
  1802. Blockly.Blocks['ai_camera_auto_gain'] = {
  1803. init: function () {
  1804. this.appendDummyInput()
  1805. .appendField(Blockly.Msg.ai_camera_set_auto_gain)
  1806. .appendField(new Blockly.FieldDropdown([
  1807. [Blockly.Msg.ai_camera_enable, "True"],
  1808. [Blockly.Msg.ai_camera_disable, "False"]
  1809. ]), "DEGREE")
  1810. this.setPreviousStatement(true, null);
  1811. this.setNextStatement(true, null);
  1812. this.setColour("#60c1bb");
  1813. this.setTooltip('');
  1814. this.setHelpUrl('');
  1815. }
  1816. };
  1817. Blockly.Python.ai_camera_auto_gain = function (block) {
  1818. var degee = block.getFieldValue('DEGREE');
  1819. var _code = "sensor.set_auto_gain(" + degee + ")\n"
  1820. return _code;
  1821. }
  1822. Blockly.Blocks['ai_camera_auto_exposure'] = {
  1823. init: function () {
  1824. this.appendDummyInput()
  1825. .appendField(Blockly.Msg.ai_camera_set_auto_exposure)
  1826. .appendField(new Blockly.FieldDropdown([
  1827. [Blockly.Msg.ai_camera_enable, "True"],
  1828. [Blockly.Msg.ai_camera_disable, "False"]
  1829. ]), "DEGREE")
  1830. this.setPreviousStatement(true, null);
  1831. this.setNextStatement(true, null);
  1832. this.setColour("#60c1bb");
  1833. this.setTooltip('');
  1834. this.setHelpUrl('');
  1835. }
  1836. };
  1837. Blockly.Python.ai_camera_auto_exposure = function (block) {
  1838. var degee = block.getFieldValue('DEGREE');
  1839. var _code = "sensor.set_auto_exposure(" + degee + ")\n"
  1840. return _code;
  1841. }
  1842. Blockly.Blocks['ai_camera_bcs_operation'] = {
  1843. init: function () {
  1844. this.appendDummyInput()
  1845. .appendField(Blockly.Msg.ai_camera_bcs_set_text)
  1846. .appendField(new Blockly.FieldDropdown([
  1847. [Blockly.Msg.ai_camera_bcs_contrast, "contrast"],
  1848. [Blockly.Msg.ai_camera_bcs_brightness, "brightness"],
  1849. [Blockly.Msg.ai_camera_bcs_saturation, "saturation"]
  1850. ]), "image_property")
  1851. .appendField(Blockly.Msg.ai_camera_bcs_as);
  1852. this.appendValueInput("A").setCheck("Number");
  1853. this.appendDummyInput()
  1854. .appendField(Blockly.Msg.ai_camera_set_range);
  1855. this.setPreviousStatement(true, null);
  1856. this.setNextStatement(true, null);
  1857. this.setInputsInline(!0);
  1858. this.setColour("#60c1bb");
  1859. var thisBlock = this;
  1860. this.setTooltip(function () {
  1861. var mode = thisBlock.getFieldValue('image_property');
  1862. var TOOLTIPS = {
  1863. 'contrast': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_contrast),
  1864. 'brightness': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_brightness),
  1865. 'saturation': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_saturation)
  1866. };
  1867. return TOOLTIPS[mode];
  1868. });
  1869. this.setHelpUrl('');
  1870. }
  1871. };
  1872. Blockly.Python.ai_camera_bcs_operation = function (block) {
  1873. var dropdown_image_property = block.getFieldValue('image_property');
  1874. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1875. var _code = "sensor.set_" + dropdown_image_property + "(" + _x + ")\n"
  1876. return _code;
  1877. }
  1878. Blockly.Blocks['ai_camera_contrast'] = {
  1879. init: function () {
  1880. this.appendDummyInput()
  1881. .appendField(Blockly.Msg.ai_camera_set_contrast_to_value);
  1882. this.appendValueInput("A").setCheck("Number");
  1883. this.appendDummyInput()
  1884. .appendField(Blockly.Msg.ai_camera_set_range);
  1885. this.setPreviousStatement(true, null);
  1886. this.setNextStatement(true, null);
  1887. this.setInputsInline(!0);
  1888. this.setColour("#60c1bb");
  1889. this.setTooltip('');
  1890. this.setHelpUrl('');
  1891. }
  1892. };
  1893. Blockly.Python.ai_camera_contrast = function (block) {
  1894. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1895. var _code = "sensor.set_contrast(" + _x + ")\n"
  1896. return _code;
  1897. }
  1898. Blockly.Blocks['ai_camera_brightness'] = {
  1899. init: function () {
  1900. this.appendDummyInput()
  1901. .appendField(Blockly.Msg.ai_camera_set_brightness_to_value);
  1902. this.appendValueInput("A").setCheck("Number");
  1903. this.appendDummyInput()
  1904. .appendField(Blockly.Msg.ai_camera_set_range);
  1905. this.setPreviousStatement(true, null);
  1906. this.setNextStatement(true, null);
  1907. this.setInputsInline(!0);
  1908. this.setColour("#60c1bb");
  1909. this.setTooltip('');
  1910. this.setHelpUrl('');
  1911. }
  1912. };
  1913. Blockly.Python.ai_camera_brightness = function (block) {
  1914. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1915. var _code = "sensor.set_brightness(" + _x + ")\n"
  1916. return _code;
  1917. }
  1918. Blockly.Blocks['ai_camera_saturation'] = {
  1919. init: function () {
  1920. this.appendDummyInput()
  1921. .appendField(Blockly.Msg.ai_camera_set_saturation_value);
  1922. this.appendValueInput("A").setCheck("Number");
  1923. this.appendDummyInput()
  1924. .appendField(Blockly.Msg.ai_camera_set_range);
  1925. this.setPreviousStatement(true, null);
  1926. this.setNextStatement(true, null);
  1927. this.setInputsInline(!0);
  1928. this.setColour("#60c1bb");
  1929. this.setTooltip('');
  1930. this.setHelpUrl('');
  1931. }
  1932. };
  1933. Blockly.Python.ai_camera_saturation = function (block) {
  1934. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1935. var _code = "sensor.set_saturation(" + _x + ")\n"
  1936. return _code;
  1937. }
  1938. /*
  1939. _ ___ __ ___ _
  1940. / \ |_ _| \ \ / (_)___(_) ___ _ __
  1941. / _ \ | | \ \ / /| / __| |/ _ \| '_ \
  1942. / ___ \ | | \ V / | \__ \ | (_) | | | |
  1943. /_/ \_\___| \_/ |_|___/_|\___/|_| |_|
  1944. */
  1945. Blockly.Blocks.ai_lcd_XY_vision = {
  1946. init: function () {
  1947. var _input = this.appendDummyInput();
  1948. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  1949. this.setColour("#f0983e");
  1950. this.setOutput(!0, "Boolean");
  1951. _input.appendField(Blockly.Msg.X);
  1952. this.appendValueInput("A").setCheck("Number");
  1953. _input = this.appendDummyInput();
  1954. _input.appendField(Blockly.Msg.Y)
  1955. this.appendValueInput("B").setCheck("Number");
  1956. this.setInputsInline(!0);
  1957. }
  1958. };
  1959. Blockly.Python.ai_lcd_XY_vision = function (block) {
  1960. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1961. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  1962. var code = "" + _x + ", " + _y + "";
  1963. return [code, Blockly.Python.ORDER_ATOMIC];
  1964. };
  1965. Blockly.Blocks.ai_lcd_WH_vision = {
  1966. init: function () {
  1967. var _input = this.appendDummyInput();
  1968. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  1969. this.setColour("#f0983e");
  1970. this.setOutput(!0, "Boolean");
  1971. _input.appendField(Blockly.Msg.W);
  1972. this.appendValueInput("A").setCheck("Number");
  1973. _input = this.appendDummyInput();
  1974. _input.appendField(Blockly.Msg.H)
  1975. this.appendValueInput("B").setCheck("Number");
  1976. this.setInputsInline(!0);
  1977. }
  1978. };
  1979. Blockly.Python.ai_lcd_WH_vision = function (block) {
  1980. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  1981. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  1982. var code = "" + _x + ", " + _y + "";
  1983. return [code, Blockly.Python.ORDER_ATOMIC];
  1984. };
  1985. Blockly.Blocks['ai_vision_color_analyze'] = {
  1986. init: function () {
  1987. this.appendDummyInput()
  1988. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_analyze_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  1989. this.appendDummyInput()
  1990. .appendField(Blockly.Msg.ai_vision_get_canvas)
  1991. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1992. .appendField(Blockly.Msg.ai_vision_color_analyze_get_result);
  1993. this.appendValueInput("xy")
  1994. .setCheck(null)
  1995. .appendField(Blockly.Msg.ai_vision_color_analyze_start_coord);
  1996. this.appendValueInput("wh")
  1997. .setCheck(null)
  1998. .appendField(Blockly.Msg.ai_vision_color_analyze_size);
  1999. this.setOutput(true, null);
  2000. this.setColour("#f0983e");
  2001. this.setTooltip(Blockly.Msg.ai_vision_color_analyze_TOOLTIP);
  2002. this.setHelpUrl("");
  2003. }
  2004. };
  2005. Blockly.Python['ai_vision_color_analyze'] = function (block) {
  2006. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2007. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2008. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2009. Blockly.Python.addFunction("function", '' +
  2010. 'def _get_regional_color_analyze_rgb(x, y, w, h):\n' +
  2011. ' _crd_bounding_box_size = (w, h)\n' +
  2012. ' _crd_r = [x, y, _crd_bounding_box_size[0], _crd_bounding_box_size[1]] # 50x50 center of QQVGA.\n' +
  2013. ' _crd_hist = ' + variable_name + '.get_statistics(bins=8,roi=_crd_r)\n' +
  2014. ' return image.lab_to_rgb((_crd_hist.l_mean(),_crd_hist.a_mean(),_crd_hist.b_mean()))\n' +
  2015. '');
  2016. // TODO: Assemble Python into code variable.
  2017. var code = '_get_regional_color_analyze_rgb(' + xy + ', ' + wh + ')';
  2018. // TODO: Change ORDER_NONE to the correct strength.
  2019. return [code, Blockly.Python.ORDER_NONE];
  2020. };
  2021. Blockly.Blocks['ai_vision_find_blobs'] = {
  2022. init: function () {
  2023. this.appendDummyInput()
  2024. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2025. this.appendDummyInput()
  2026. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2027. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2028. .appendField(Blockly.Msg.ai_vision_find_blobs_color_region);
  2029. this.appendValueInput("color_threshold")
  2030. .setCheck(null)
  2031. .appendField(Blockly.Msg.ai_vision_find_blobs_color_threshold);
  2032. this.appendValueInput("xy")
  2033. .setCheck(null)
  2034. .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
  2035. this.appendValueInput("wh")
  2036. .setCheck(null)
  2037. .appendField(Blockly.Msg.ai_vision_find_blobs_size);
  2038. /*this.appendValueInput("area_threshold")
  2039. .setCheck(null)
  2040. .appendField(Blockly.Msg.ai_vision_find_blobs_region_threshold)
  2041. .appendField(new Blockly.FieldCheckbox("FALSE"), "check_area");
  2042. this.appendValueInput("pixels_threshold")
  2043. .setCheck(null)
  2044. .appendField(Blockly.Msg.ai_vision_find_blobs_pixel_threshold)
  2045. .appendField(new Blockly.FieldCheckbox("FALSE"), "check_pixels");
  2046. this.appendValueInput("margin")
  2047. .setCheck(null)
  2048. .appendField(Blockly.Msg.ai_vision_find_blobs_margin)
  2049. .appendField(new Blockly.FieldCheckbox("FALSE"), "margin");
  2050. this.appendDummyInput()
  2051. .appendField(Blockly.Msg.ai_vision_find_blobs_filtered_region)
  2052. .appendField(new Blockly.FieldCheckbox("FALSE"), "check_merge");*/
  2053. this.setInputsInline(false);
  2054. this.setOutput(true, null);
  2055. this.setColour("#f0983e");
  2056. this.setTooltip(Blockly.Msg.ai_vision_find_blobs_TOOLTIP);
  2057. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2058. }
  2059. };
  2060. Blockly.Python['ai_vision_find_blobs'] = function (block) {
  2061. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2062. var value_color_threshold = Blockly.Python.valueToCode(block, 'color_threshold', Blockly.Python.ORDER_ATOMIC);
  2063. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2064. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2065. /* var value_area_threshold = Blockly.Python.valueToCode(block, 'area_threshold', Blockly.Python.ORDER_ATOMIC);
  2066. var value_pixels_threshold = Blockly.Python.valueToCode(block, 'pixels_threshold', Blockly.Python.ORDER_ATOMIC);
  2067. var checkbox_check_area = block.getFieldValue('check_area') == 'TRUE';
  2068. var checkbox_check_pixels = block.getFieldValue('check_pixels') == 'TRUE';
  2069. var checkbox_check_merge = block.getFieldValue('check_merge') == 'TRUE';
  2070. var value_margin = Blockly.Python.valueToCode(block, 'margin', Blockly.Python.ORDER_ATOMIC);
  2071. var checkbox_margin = block.getFieldValue('margin') == 'TRUE';
  2072. var area = checkbox_check_area;
  2073. var pixels = checkbox_check_pixels;
  2074. var margin = checkbox_margin;
  2075. var merge = checkbox_check_merge.toString();
  2076. var merge_state = merge.charAt(0).toUpperCase() + merge.slice(1);
  2077. if (margin == false) {
  2078. if (area == false && pixels == false) {
  2079. var addtional_parameters = ' ';
  2080. } else if (area == true && pixels == false) {
  2081. var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold;
  2082. } else if (area == false && pixels == true) {
  2083. var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold;
  2084. } else if (area == true && pixels == true) {
  2085. var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold;
  2086. }
  2087. } else if (margin == true) {
  2088. if (area == false && pixels == false) {
  2089. var addtional_parameters = ', margin=' + value_margin;
  2090. } else if (area == true && pixels == false) {
  2091. var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold + ', margin=' + value_margin;
  2092. } else if (area == false && pixels == true) {
  2093. var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', margin=' + value_margin;
  2094. } else if (area == true && pixels == true) {
  2095. var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold + ', margin=' + value_margin;
  2096. }
  2097. }
  2098. // TODO: Assemble Python into code variable.
  2099. var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=' + merge_state + addtional_parameters + ')';*/
  2100. var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=False, area_threshold=150)';
  2101. // TODO: Change ORDER_NONE to the correct strength.
  2102. return [code, Blockly.Python.ORDER_NONE];
  2103. };
  2104. Blockly.Blocks['ai_vision_get_blobs'] = {
  2105. init: function () {
  2106. this.appendDummyInput()
  2107. .appendField(Blockly.Msg.ai_vision_from)
  2108. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2109. .appendField(Blockly.Msg.ai_vision_get_blobs_result)
  2110. .appendField(new Blockly.FieldDropdown([
  2111. [Blockly.Msg.ai_vision_detection_box_x, "x()"],
  2112. [Blockly.Msg.ai_vision_detection_box_y, "y()"],
  2113. [Blockly.Msg.ai_vision_detection_box_width, "w()"],
  2114. [Blockly.Msg.ai_vision_detection_box_height, "h()"],
  2115. [Blockly.Msg.ai_vision_detection_box_pixels, "pixels()"],
  2116. [Blockly.Msg.ai_vision_detection_box_cx, "cx()"],
  2117. [Blockly.Msg.ai_vision_detection_box_cy, "cy()"],
  2118. [Blockly.Msg.ai_vision_detection_box_degree, "rotation()"],
  2119. [Blockly.Msg.ai_vision_detection_box_total, "count()"]
  2120. ]), "blob_type");
  2121. this.setInputsInline(false);
  2122. this.setOutput(true, null);
  2123. this.setColour("#f0983e");
  2124. this.setTooltip(Blockly.Msg.ai_vision_get_blobs_TOOLTIP);
  2125. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2126. }
  2127. };
  2128. Blockly.Python['ai_vision_get_blobs'] = function (block) {
  2129. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2130. var type = block.getFieldValue('blob_type');
  2131. // TODO: Assemble Python into code variable.
  2132. var code = variable_name + '.' + type;
  2133. // TODO: Change ORDER_NONE to the correct strength.
  2134. return [code, Blockly.Python.ORDER_NONE];
  2135. };
  2136. Blockly.Blocks['ai_vision_get_threshold_default'] = {
  2137. init: function () {
  2138. this.appendDummyInput()
  2139. .appendField(Blockly.Msg.ai_vision_detection_color_threshold_set)
  2140. .appendField(new Blockly.FieldDropdown([
  2141. [Blockly.Msg.ai_vision_detection_color_threshold_set_black, "black"],
  2142. [Blockly.Msg.ai_vision_detection_color_threshold_set_white, "white"],
  2143. [Blockly.Msg.ai_vision_detection_color_threshold_set_red, "red"],
  2144. [Blockly.Msg.ai_vision_detection_color_threshold_set_green, "green"],
  2145. [Blockly.Msg.ai_vision_detection_color_threshold_set_blue, "blue"],
  2146. [Blockly.Msg.ai_vision_detection_color_threshold_set_orange, "orange"]
  2147. ]), "color");
  2148. this.setOutput(true, null);
  2149. this.setColour("#f0983e");
  2150. var thisBlock = this;
  2151. this.setTooltip(function () {
  2152. var mode = thisBlock.getFieldValue('color');
  2153. var TOOLTIPS = {
  2154. 'black': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_black),
  2155. 'white': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_white),
  2156. 'red': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_red),
  2157. 'green': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_green),
  2158. 'blue': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_blue),
  2159. 'orange': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_orange)
  2160. };
  2161. return TOOLTIPS[mode];
  2162. });
  2163. this.setHelpUrl("");
  2164. }
  2165. };
  2166. Blockly.Python['ai_vision_get_threshold_default'] = function (block) {
  2167. var dropdown_color = block.getFieldValue('color');
  2168. // TODO: Assemble Python into code variable.
  2169. if (dropdown_color == "black") {
  2170. var code = '[(0,40)]';
  2171. } else if (dropdown_color == "white") {
  2172. var code = '[(128,255)]';
  2173. } else if (dropdown_color == "red") {
  2174. var code = '[(45, 65, 40, 80, 40, 60)]';
  2175. } else if (dropdown_color == "green") {
  2176. var code = '[(45, 65, -50, -30, 0, 40)]';
  2177. } else if (dropdown_color == "blue") {
  2178. var code = '[(45, 65, -20, 30, -60, -20)]';
  2179. } else if (dropdown_color == "orange") {
  2180. var code = '[(77, 55, 19, 61, 14, 108)]';
  2181. }
  2182. // TODO: Change ORDER_NONE to the correct strength.
  2183. return [code, Blockly.Python.ORDER_NONE];
  2184. };
  2185. Blockly.Blocks['ai_vision_find_qrcode'] = {
  2186. init: function () {
  2187. this.appendDummyInput()
  2188. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_qrcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2189. this.appendDummyInput()
  2190. .appendField(Blockly.Msg.ai_vision_get_canvas)
  2191. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2192. .appendField(Blockly.Msg.ai_vision_qrcode_detected_result);
  2193. this.setOutput(true, null);
  2194. this.setColour("#f0983e");
  2195. this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
  2196. this.setHelpUrl("");
  2197. }
  2198. };
  2199. Blockly.Python['ai_vision_find_qrcode'] = function (block) {
  2200. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2201. // TODO: Assemble Python into code variable.
  2202. var code = variable_name + '.find_qrcodes()';
  2203. // TODO: Change ORDER_NONE to the correct strength.
  2204. return [code, Blockly.Python.ORDER_NONE];
  2205. };
  2206. Blockly.Blocks['ai_vision_qrcode_get_info'] = {
  2207. init: function () {
  2208. this.appendDummyInput()
  2209. .appendField(Blockly.Msg.ai_vision_from)
  2210. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2211. .appendField(Blockly.Msg.ai_vision_qrcode_get_result)
  2212. .appendField(new Blockly.FieldDropdown([
  2213. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  2214. [Blockly.Msg.ai_vision_pattern_detection_type, "data_type"],
  2215. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  2216. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  2217. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  2218. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  2219. ]), "get_info");
  2220. this.setInputsInline(false);
  2221. this.setOutput(true, null);
  2222. this.setColour("#f0983e");
  2223. var thisBlock = this;
  2224. this.setTooltip(function () {
  2225. var mode = thisBlock.getFieldValue('get_info');
  2226. var TOOLTIPS = {
  2227. 'payload': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  2228. 'data_type': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
  2229. 'x': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  2230. 'y': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  2231. 'w': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  2232. 'h': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  2233. };
  2234. return TOOLTIPS[mode];
  2235. });
  2236. this.setHelpUrl("");
  2237. }
  2238. };
  2239. Blockly.Python['ai_vision_qrcode_get_info'] = function (block) {
  2240. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2241. var dropdown_name = block.getFieldValue('get_info');
  2242. // TODO: Assemble Python into code variable.
  2243. var code = variable_name + '.' + dropdown_name + '()';
  2244. // TODO: Change ORDER_NONE to the correct strength.
  2245. return [code, Blockly.Python.ORDER_NONE];
  2246. };
  2247. Blockly.Blocks['ai_vision_find_apriltag'] = {
  2248. init: function () {
  2249. this.appendDummyInput()
  2250. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_apriltag_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2251. this.appendDummyInput()
  2252. .appendField(Blockly.Msg.ai_vision_get_canvas)
  2253. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2254. .appendField(Blockly.Msg.ai_vision_apriltag_detected_result);
  2255. this.setOutput(true, null);
  2256. this.setColour("#f0983e");
  2257. this.setTooltip(Blockly.Msg.ai_vision_find_apriltag_TOOLTIP);
  2258. this.setHelpUrl("");
  2259. }
  2260. };
  2261. Blockly.Python['ai_vision_find_apriltag'] = function (block) {
  2262. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2263. // TODO: Assemble Python into code variable.
  2264. var code = variable_name + '.find_apriltags()';
  2265. // TODO: Change ORDER_NONE to the correct strength.
  2266. return [code, Blockly.Python.ORDER_NONE];
  2267. };
  2268. Blockly.Blocks['ai_vision_apriltag_get_info'] = {
  2269. init: function () {
  2270. this.appendDummyInput()
  2271. .appendField(Blockly.Msg.ai_vision_from)
  2272. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2273. .appendField(Blockly.Msg.ai_vision_apriltag_get_result)
  2274. .appendField(new Blockly.FieldDropdown([
  2275. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  2276. [Blockly.Msg.ai_vision_pattern_detection_type_id, "id"],
  2277. [Blockly.Msg.ai_vision_pattern_detection_type_family, "family"],
  2278. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  2279. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  2280. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  2281. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  2282. ]), "get_info");
  2283. this.setInputsInline(false);
  2284. this.setOutput(true, null);
  2285. this.setColour("#f0983e");
  2286. var thisBlock = this;
  2287. this.setTooltip(function () {
  2288. var mode = thisBlock.getFieldValue('get_info');
  2289. var TOOLTIPS = {
  2290. 'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  2291. 'id': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_id),
  2292. 'family': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_family),
  2293. 'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  2294. 'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  2295. 'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  2296. 'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  2297. };
  2298. return TOOLTIPS[mode];
  2299. });
  2300. this.setHelpUrl("");
  2301. }
  2302. };
  2303. Blockly.Python['ai_vision_apriltag_get_info'] = function (block) {
  2304. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2305. var dropdown_name = block.getFieldValue('get_info');
  2306. // TODO: Assemble Python into code variable.
  2307. var code = variable_name + '.' + dropdown_name + '()';
  2308. // TODO: Change ORDER_NONE to the correct strength.
  2309. return [code, Blockly.Python.ORDER_NONE];
  2310. };
  2311. Blockly.Blocks['ai_vision_find_datamatrices'] = {
  2312. init: function () {
  2313. this.appendDummyInput()
  2314. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_datamatrix_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2315. this.appendDummyInput()
  2316. .appendField(Blockly.Msg.ai_vision_get_canvas)
  2317. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2318. .appendField(Blockly.Msg.ai_vision_datamatrices_detected_result);
  2319. this.setOutput(true, null);
  2320. this.setColour("#f0983e");
  2321. this.setTooltip(Blockly.Msg.ai_vision_find_datamatrices_TOOLTIP);
  2322. this.setHelpUrl("");
  2323. }
  2324. };
  2325. Blockly.Python['ai_vision_find_datamatrices'] = function (block) {
  2326. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2327. // TODO: Assemble Python into code variable.
  2328. var code = variable_name + '.find_datamatrices()';
  2329. // TODO: Change ORDER_NONE to the correct strength.
  2330. return [code, Blockly.Python.ORDER_NONE];
  2331. };
  2332. Blockly.Blocks['ai_vision_datamatrices_get_info'] = {
  2333. init: function () {
  2334. this.appendDummyInput()
  2335. .appendField(Blockly.Msg.ai_vision_from)
  2336. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2337. .appendField(Blockly.Msg.ai_vision_data_matrices_get_result)
  2338. .appendField(new Blockly.FieldDropdown([
  2339. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  2340. [Blockly.Msg.ai_vision_pattern_detection_rows, "rows"],
  2341. [Blockly.Msg.ai_vision_pattern_detection_cols, "columns"],
  2342. [Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count, "capacity"],
  2343. [Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count, "padding"],
  2344. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  2345. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  2346. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  2347. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  2348. ]), "get_info");
  2349. this.setInputsInline(false);
  2350. this.setOutput(true, null);
  2351. this.setColour("#f0983e");
  2352. var thisBlock = this;
  2353. this.setTooltip(function () {
  2354. var mode = thisBlock.getFieldValue('get_info');
  2355. var TOOLTIPS = {
  2356. 'payload': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  2357. 'rows': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_rows),
  2358. 'columns': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_cols),
  2359. 'capacity': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count),
  2360. 'padding': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count),
  2361. 'x': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  2362. 'y': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  2363. 'w': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  2364. 'h': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  2365. };
  2366. return TOOLTIPS[mode];
  2367. });
  2368. this.setHelpUrl("");
  2369. }
  2370. };
  2371. Blockly.Python['ai_vision_datamatrices_get_info'] = function (block) {
  2372. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2373. var dropdown_name = block.getFieldValue('get_info');
  2374. // TODO: Assemble Python into code variable.
  2375. var code = variable_name + '.' + dropdown_name + '()';
  2376. // TODO: Change ORDER_NONE to the correct strength.
  2377. return [code, Blockly.Python.ORDER_NONE];
  2378. };
  2379. Blockly.Blocks['ai_vision_find_barcodes'] = {
  2380. init: function () {
  2381. this.appendDummyInput()
  2382. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_barcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2383. this.appendDummyInput()
  2384. .appendField(Blockly.Msg.ai_vision_get_canvas)
  2385. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2386. .appendField(Blockly.Msg.ai_vision_barcode_detected_result);
  2387. this.setOutput(true, null);
  2388. this.setColour("#f0983e");
  2389. this.setTooltip(Blockly.Msg.ai_vision_find_barcodes_TOOLTIP);
  2390. this.setHelpUrl("");
  2391. }
  2392. };
  2393. Blockly.Python['ai_vision_find_barcodes'] = function (block) {
  2394. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2395. // TODO: Assemble Python into code variable.
  2396. var code = variable_name + '.find_barcodes()';
  2397. // TODO: Change ORDER_NONE to the correct strength.
  2398. return [code, Blockly.Python.ORDER_NONE];
  2399. };
  2400. Blockly.Blocks['ai_vision_barcodes_get_info'] = {
  2401. init: function () {
  2402. this.appendDummyInput()
  2403. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2404. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2405. .appendField(Blockly.Msg.ai_vision_barcode_matrices_get_result)
  2406. .appendField(new Blockly.FieldDropdown([
  2407. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  2408. [Blockly.Msg.ai_vision_pattern_detection_type, "type"],
  2409. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  2410. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  2411. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  2412. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  2413. ]), "get_info");
  2414. this.setInputsInline(false);
  2415. this.setOutput(true, null);
  2416. this.setColour("#f0983e");
  2417. var thisBlock = this;
  2418. this.setTooltip(function () {
  2419. var mode = thisBlock.getFieldValue('get_info');
  2420. var TOOLTIPS = {
  2421. 'payload': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  2422. 'type': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
  2423. 'x': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  2424. 'y': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  2425. 'w': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  2426. 'h': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  2427. };
  2428. return TOOLTIPS[mode];
  2429. });
  2430. this.setHelpUrl("");
  2431. }
  2432. };
  2433. Blockly.Python['ai_vision_barcodes_get_info'] = function (block) {
  2434. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2435. var dropdown_name = block.getFieldValue('get_info');
  2436. // TODO: Assemble Python into code variable.
  2437. var code = variable_name + '.' + dropdown_name + '()';
  2438. // TODO: Change ORDER_NONE to the correct strength.
  2439. return [code, Blockly.Python.ORDER_NONE];
  2440. };
  2441. Blockly.Blocks['ai_vision_find_circles'] = {
  2442. init: function () {
  2443. this.appendDummyInput()
  2444. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_circle.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2445. this.appendDummyInput()
  2446. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2447. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2448. .appendField(Blockly.Msg.ai_vision_find_circle);
  2449. this.appendValueInput("xy")
  2450. .setCheck(null)
  2451. .appendField(Blockly.Msg.ai_vision_find_circle_start_coord);
  2452. this.appendValueInput("wh")
  2453. .setCheck(null)
  2454. .appendField(Blockly.Msg.ai_vision_find_circle_region_size);
  2455. /*this.appendValueInput("threshold")
  2456. .setCheck(null)
  2457. .appendField(Blockly.Msg.ai_vision_find_circle_hoffman_threshold);
  2458. this.appendValueInput("x_margin")
  2459. .setCheck(null)
  2460. .appendField(Blockly.Msg.ai_vision_find_circle_merge_x);
  2461. this.appendValueInput("y_margin")
  2462. .setCheck(null)
  2463. .appendField(Blockly.Msg.ai_vision_find_circle_merge_y);
  2464. this.appendValueInput("r_margin")
  2465. .setCheck(null)
  2466. .appendField(Blockly.Msg.ai_vision_find_circle_merge_r);
  2467. this.appendValueInput("r_min")
  2468. .setCheck(null)
  2469. .appendField(Blockly.Msg.ai_vision_find_circle_min_radius);
  2470. this.appendValueInput("r_max")
  2471. .setCheck(null)
  2472. .appendField(Blockly.Msg.ai_vision_find_circle_max_radius);
  2473. this.appendValueInput("r_step")
  2474. .setCheck(null)
  2475. .appendField(Blockly.Msg.ai_vision_find_circle_step_radius);*/
  2476. this.setOutput(true, null);
  2477. this.setColour("#f0983e");
  2478. this.setTooltip(Blockly.Msg.ai_vision_find_circles_TOOLTIP);
  2479. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2480. }
  2481. };
  2482. Blockly.Python['ai_vision_find_circles'] = function (block) {
  2483. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2484. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2485. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2486. /*var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
  2487. var x_margin = Blockly.Python.valueToCode(block, 'x_margin', Blockly.Python.ORDER_ATOMIC);
  2488. var y_margin = Blockly.Python.valueToCode(block, 'y_margin', Blockly.Python.ORDER_ATOMIC);
  2489. var r_margin = Blockly.Python.valueToCode(block, 'r_margin', Blockly.Python.ORDER_ATOMIC);
  2490. var r_min = Blockly.Python.valueToCode(block, 'r_min', Blockly.Python.ORDER_ATOMIC);
  2491. var r_max = Blockly.Python.valueToCode(block, 'r_max', Blockly.Python.ORDER_ATOMIC);
  2492. var r_step = Blockly.Python.valueToCode(block, 'r_step', Blockly.Python.ORDER_ATOMIC);*/
  2493. // TODO: Assemble Python into code variable.
  2494. // var code = variable_name + '.find_circles(roi=(' + xy + ',' + wh + '), threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 2, r_max = 100, r_step = 2)';
  2495. var code = variable_name + '.find_circles(roi=(' + xy + ',' + wh + '), threshold = 2500, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 10, r_max = 100, r_step = 2)';
  2496. // TODO: Change ORDER_NONE to the correct strength.
  2497. return [code, Blockly.Python.ORDER_NONE];
  2498. };
  2499. Blockly.Blocks['ai_vision_get_circles'] = {
  2500. init: function () {
  2501. this.appendDummyInput()
  2502. .appendField(Blockly.Msg.ai_vision_from)
  2503. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2504. .appendField(Blockly.Msg.ai_vision_find_circle_detected_result)
  2505. .appendField(new Blockly.FieldDropdown([
  2506. [Blockly.Msg.ai_vision_find_circle_detected_x, "x()"],
  2507. [Blockly.Msg.ai_vision_find_circle_detected_y, "y()"],
  2508. [Blockly.Msg.ai_vision_find_circle_detected_radius, "r()"]
  2509. ]), "blob_type");
  2510. this.setInputsInline(false);
  2511. this.setOutput(true, null);
  2512. this.setColour("#f0983e");
  2513. this.setTooltip(Blockly.Msg.ai_vision_get_circles_TOOLTIP);
  2514. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2515. }
  2516. };
  2517. Blockly.Python['ai_vision_get_circles'] = function (block) {
  2518. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2519. var type = block.getFieldValue('blob_type');
  2520. var code = variable_name + '.' + type;
  2521. return [code, Blockly.Python.ORDER_NONE];
  2522. };
  2523. Blockly.Blocks['ai_vision_find_rects'] = {
  2524. init: function () {
  2525. this.appendDummyInput()
  2526. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_rect.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2527. this.appendDummyInput()
  2528. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2529. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2530. .appendField(Blockly.Msg.ai_vision_find_rect);
  2531. this.appendValueInput("xy")
  2532. .setCheck(null)
  2533. .appendField(Blockly.Msg.ai_vision_find_rect_start_coord);
  2534. this.appendValueInput("wh")
  2535. .setCheck(null)
  2536. .appendField(Blockly.Msg.ai_vision_find_rect_region_size);
  2537. /*this.appendValueInput("threshold")
  2538. .setCheck(null)
  2539. .appendField(Blockly.Msg.ai_vision_find_rect_bounding_threshold);*/
  2540. this.setOutput(true, null);
  2541. this.setColour("#f0983e");
  2542. this.setTooltip(Blockly.Msg.ai_vision_find_rects_TOOLTIP);
  2543. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2544. }
  2545. };
  2546. Blockly.Python['ai_vision_find_rects'] = function (block) {
  2547. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2548. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2549. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2550. // var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
  2551. // TODO: Assemble Python into code variable.
  2552. var code = variable_name + '.find_rects(roi=(' + xy + ',' + wh + '), threshold = 10000)';
  2553. // TODO: Change ORDER_NONE to the correct strength.
  2554. return [code, Blockly.Python.ORDER_NONE];
  2555. };
  2556. Blockly.Blocks['ai_vision_get_rects'] = {
  2557. init: function () {
  2558. this.appendDummyInput()
  2559. .appendField(Blockly.Msg.ai_vision_from)
  2560. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2561. .appendField(Blockly.Msg.ai_vision_find_rect_detected_result)
  2562. .appendField(new Blockly.FieldDropdown([
  2563. [Blockly.Msg.ai_vision_find_rect_detected_x, "x()"],
  2564. [Blockly.Msg.ai_vision_find_rect_detected_y, "y()"],
  2565. [Blockly.Msg.ai_vision_find_rect_detected_w, "w()"],
  2566. [Blockly.Msg.ai_vision_find_rect_detected_h, "h()"],
  2567. [Blockly.Msg.ai_vision_find_rect_detected_tuple, "rect()"],
  2568. [Blockly.Msg.ai_vision_find_rect_detected_corners, "corners()"]
  2569. ]), "blob_type");
  2570. this.setInputsInline(false);
  2571. this.setOutput(true, null);
  2572. this.setColour("#f0983e");
  2573. this.setTooltip(Blockly.Msg.ai_vision_get_rects_TOOLTIP);
  2574. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2575. }
  2576. };
  2577. Blockly.Python['ai_vision_get_rects'] = function (block) {
  2578. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2579. var type = block.getFieldValue('blob_type');
  2580. // TODO: Assemble Python into code variable.
  2581. var code = variable_name + '.' + type;
  2582. // TODO: Change ORDER_NONE to the correct strength.
  2583. return [code, Blockly.Python.ORDER_NONE];
  2584. };
  2585. Blockly.Blocks['ai_vision_find_lines'] = {
  2586. init: function () {
  2587. this.appendDummyInput()
  2588. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_line.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2589. this.appendDummyInput()
  2590. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2591. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2592. .appendField(Blockly.Msg.ai_vision_find_line);
  2593. this.appendValueInput("xy")
  2594. .setCheck(null)
  2595. .appendField(Blockly.Msg.ai_vision_find_line_start_coord);
  2596. this.appendValueInput("wh")
  2597. .setCheck(null)
  2598. .appendField(Blockly.Msg.ai_vision_find_line_size);
  2599. /*this.appendValueInput("threshold")
  2600. .setCheck(null)
  2601. .appendField(Blockly.Msg.ai_vision_find_line_hoffman_threshold);
  2602. this.appendValueInput("theta_margin")
  2603. .setCheck(null)
  2604. .appendField(Blockly.Msg.ai_vision_find_line_merge_theta);
  2605. this.appendValueInput("rho_margin")
  2606. .setCheck(null)
  2607. .appendField(Blockly.Msg.ai_vision_find_line_merge_rho);*/
  2608. this.setOutput(true, null);
  2609. this.setColour("#f0983e");
  2610. this.setTooltip(Blockly.Msg.ai_vision_find_lines_TOOLTIP);
  2611. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2612. }
  2613. };
  2614. Blockly.Python['ai_vision_find_lines'] = function (block) {
  2615. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2616. //var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
  2617. //var theta_margin = Blockly.Python.valueToCode(block, 'theta_margin', Blockly.Python.ORDER_ATOMIC);
  2618. //var rho_margin = Blockly.Python.valueToCode(block, 'rho_margin', Blockly.Python.ORDER_ATOMIC);
  2619. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2620. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2621. // TODO: Assemble Python into code variable.
  2622. var code = variable_name + '.find_lines(roi=(' + xy + ',' + wh + '), threshold = 1000, theta_margin = 25, rho_margin = 25)';
  2623. // TODO: Change ORDER_NONE to the correct strength.
  2624. return [code, Blockly.Python.ORDER_NONE];
  2625. };
  2626. Blockly.Blocks['ai_vision_get_lines'] = {
  2627. init: function () {
  2628. this.appendDummyInput()
  2629. .appendField(Blockly.Msg.ai_vision_from)
  2630. .appendField(new Blockly.FieldVariable("i"), "varitem")
  2631. .appendField(Blockly.Msg.ai_vision_find_line_detected_result)
  2632. .appendField(new Blockly.FieldDropdown([
  2633. [Blockly.Msg.ai_vision_find_line_detected_start_x, "x1()"],
  2634. [Blockly.Msg.ai_vision_find_line_detected_start_y, "y1()"],
  2635. [Blockly.Msg.ai_vision_find_line_detected_end_x, "x2()"],
  2636. [Blockly.Msg.ai_vision_find_line_detected_end_y, "y2()"],
  2637. [Blockly.Msg.ai_vision_find_line_detected_length, "length()"],
  2638. [Blockly.Msg.ai_vision_find_line_detected_angle, "theta()"]
  2639. ]), "blob_type");
  2640. this.setInputsInline(false);
  2641. this.setOutput(true, null);
  2642. this.setColour("#f0983e");
  2643. this.setTooltip(Blockly.Msg.ai_vision_get_lines_TOOLTIP);
  2644. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2645. }
  2646. };
  2647. Blockly.Python['ai_vision_get_lines'] = function (block) {
  2648. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2649. var type = block.getFieldValue('blob_type');
  2650. // TODO: Assemble Python into code variable.
  2651. var code = variable_name + '.' + type;
  2652. // TODO: Change ORDER_NONE to the correct strength.
  2653. return [code, Blockly.Python.ORDER_NONE];
  2654. };
  2655. /*
  2656. _ ___ __ __ _ _
  2657. / \ |_ _| | \/ (_) ___ _ __ ___ _ __ | |__ ___ _ __ ___
  2658. / _ \ | | | |\/| | |/ __| '__/ _ \| '_ \| '_ \ / _ \| '_ \ / _ \
  2659. / ___ \ | | | | | | | (__| | | (_) | |_) | | | | (_) | | | | __/
  2660. /_/ \_\___| |_| |_|_|\___|_| \___/| .__/|_| |_|\___/|_| |_|\___|
  2661. |_|
  2662. */
  2663. Blockly.Blocks['ai_mphone_init'] = {
  2664. init: function () {
  2665. this.appendDummyInput()
  2666. .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2667. this.appendDummyInput()
  2668. .appendField(Blockly.Msg.ai_microphone_init);
  2669. this.setPreviousStatement(true);
  2670. this.setNextStatement(true);
  2671. this.setColour("#5fcd8e");
  2672. this.setTooltip(Blockly.Msg.ai_mphone_init_TOOLTIP);
  2673. this.setHelpUrl("");
  2674. }
  2675. };
  2676. Blockly.Python.ai_mphone_init = function (block) {
  2677. Blockly.Python.definitions_.import_all_mphone = "" +
  2678. "from Maix import GPIO, I2S, FFT\n" +
  2679. "from board import board_info\n" +
  2680. "from fpioa_manager import fm\n" +
  2681. "\n" +
  2682. "sample_rate = 38640\n" +
  2683. "sample_points = 1024\n" +
  2684. "fft_points = 512\n" +
  2685. "hist_x_num = 50\n" +
  2686. "\n" +
  2687. "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  2688. "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
  2689. "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
  2690. "\n" +
  2691. "_recorder_rx = I2S(I2S.DEVICE_0)\n" +
  2692. "_recorder_rx.channel_config(_recorder_rx.CHANNEL_0, _recorder_rx.RECEIVER, align_mode = I2S.STANDARD_MODE)\n" +
  2693. "_recorder_rx.set_sample_rate(sample_rate)\n" +
  2694. "def _microphone_read_average(lst): \n" +
  2695. " return int((sum(lst)/len(lst))*100)\n" +
  2696. "";
  2697. Blockly.Python.addVariable('sample_rate', "sample_rate = 38640", true);
  2698. Blockly.Python.addVariable('sample_points', "sample_points = 1024", true);
  2699. Blockly.Python.addVariable('fft_points', "fft_points = 512", true);
  2700. Blockly.Python.addVariable('hist_x_num', "hist_x_num = 50", true);
  2701. Blockly.Python.addVariable('_recorder_rx', "", true);
  2702. Blockly.Python.addVariable('fft_res', "", true);
  2703. Blockly.Python.addVariable('fft_amp', "", true);
  2704. Blockly.Python.addVariable('read_all_channel', "read_all_channel = [0, 0, 0, 0, 0, 0, 0, 0]", true);
  2705. // Blockly.Python.addVariable('_recorder_rx', "", true);
  2706. var _code = "" +
  2707. "";
  2708. return _code;
  2709. }
  2710. Blockly.Blocks['mphone_audio_spectrum_init'] = {
  2711. init: function () {
  2712. this.appendDummyInput()
  2713. .appendField(new Blockly.FieldImage("blockly/media/ai_audio_spectrum.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2714. this.appendDummyInput()
  2715. .appendField(Blockly.Msg.ai_microphone_start_spectrum_analyze);
  2716. this.setPreviousStatement(true, null);
  2717. this.setNextStatement(true, null);
  2718. this.setColour("#5fcd8e");
  2719. this.setTooltip(Blockly.Msg.mphone_audio_spectrum_init_TOOLTIP);
  2720. this.setHelpUrl("");
  2721. }
  2722. };
  2723. Blockly.Python['mphone_audio_spectrum_init'] = function (block) {
  2724. // TODO: Assemble Python into code variable.
  2725. Blockly.Python.definitions_.mphone_audio_spectrum_init_def = "" +
  2726. "import time\n" +
  2727. "";
  2728. var code = "" +
  2729. "time.sleep_ms(10)\n" +
  2730. "_audio = _recorder_rx.record(sample_points)\n" +
  2731. "fft_res = FFT.run(_audio.to_bytes(),fft_points)\n" +
  2732. "fft_amp = FFT.amplitude(fft_res)\n\n" +
  2733. "read_all_channel[0] = int(_microphone_read_average(fft_amp[0:63]))\n" +
  2734. "read_all_channel[1] = int(_microphone_read_average(fft_amp[64:127]))\n" +
  2735. "read_all_channel[2] = int(_microphone_read_average(fft_amp[127:191]))\n" +
  2736. "read_all_channel[3] = int(_microphone_read_average(fft_amp[192:255]))\n" +
  2737. "read_all_channel[4] = int(_microphone_read_average(fft_amp[256:319]))\n" +
  2738. "read_all_channel[5] = int(_microphone_read_average(fft_amp[320:383]))\n" +
  2739. "read_all_channel[6] = int(_microphone_read_average(fft_amp[384:447]))\n" +
  2740. "read_all_channel[7] = int(_microphone_read_average(fft_amp[448:514]))\n" +
  2741. "";
  2742. return code;
  2743. };
  2744. Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
  2745. init: function () {
  2746. this.appendDummyInput()
  2747. .appendField(Blockly.Msg.ai_microphone_get_spectrum_analyze_result)
  2748. .appendField(new Blockly.FieldDropdown([
  2749. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_1, "0"],
  2750. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_2, "1"],
  2751. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_3, "2"],
  2752. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_4, "3"],
  2753. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_5, "4"],
  2754. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_6, "5"],
  2755. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_7, "6"],
  2756. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_8, "7"]
  2757. ]), "level");
  2758. this.setOutput(true, null);
  2759. this.setColour("#5fcd8e");
  2760. this.setTooltip(Blockly.Msg.ai_mphone_read_sound_sensitivity_TOOLTIP);
  2761. this.setHelpUrl("");
  2762. }
  2763. };
  2764. Blockly.Python['ai_mphone_read_sound_sensitivity'] = function (block) {
  2765. var dropdown_level = block.getFieldValue('level');
  2766. // TODO: Assemble Python into code variable.
  2767. var code = 'read_all_channel[' + dropdown_level + ']';
  2768. // TODO: Change ORDER_NONE to the correct strength.
  2769. return [code, Blockly.Python.ORDER_NONE];
  2770. };
  2771. /*
  2772. _ ___ _ _ _
  2773. / \ |_ _| / \ _ _ __| (_) ___
  2774. / _ \ | | / _ \| | | |/ _` | |/ _ \
  2775. / ___ \ | | / ___ \ |_| | (_| | | (_) |
  2776. /_/ \_\___| /_/ \_\__,_|\__,_|_|\___/
  2777. */
  2778. Blockly.Blocks['ai_audio_init'] = {
  2779. init: function () {
  2780. this.appendDummyInput()
  2781. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2782. this.appendDummyInput()
  2783. .appendField(Blockly.Msg.ai_audio_init);
  2784. this.setColour("#603ea0");
  2785. this.setTooltip("");
  2786. this.setHelpUrl("");
  2787. this.setPreviousStatement(true);
  2788. this.setNextStatement(true);
  2789. }
  2790. };
  2791. Blockly.Python.ai_audio_init = function (block) {
  2792. Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
  2793. Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
  2794. Blockly.Python.definitions_.import_audio = "import audio";
  2795. var _code = "";
  2796. return _code;
  2797. }
  2798. Blockly.Blocks['ai_audio_play_song'] = {
  2799. init: function () {
  2800. this.appendDummyInput()
  2801. .appendField(Blockly.Msg.ai_audio_play_music_file);
  2802. this.appendDummyInput()
  2803. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  2804. .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  2805. this.appendValueInput("path")
  2806. .setCheck(null)
  2807. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  2808. this.setPreviousStatement(true, null);
  2809. this.setNextStatement(true, null);
  2810. this.setColour("#603ea0");
  2811. this.setTooltip("");
  2812. this.setHelpUrl("");
  2813. }
  2814. };
  2815. Blockly.Python['ai_audio_play_song'] = function (block) {
  2816. var number_play_vol = block.getFieldValue('play_vol');
  2817. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  2818. var _in = "" +
  2819. "_audio_rx = I2S(I2S.DEVICE_0)\n" +
  2820. "\n" +
  2821. "def _speaker_play_song(path, volume):\n" +
  2822. " global _audio_play_state\n" +
  2823. " _player = audio.Audio(path = path)\n" +
  2824. " _player.volume(volume)\n" +
  2825. " _wav_info = _player.play_process(_audio_rx)\n" +
  2826. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  2827. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  2828. " _audio_play_state = 0\n" +
  2829. "\n" +
  2830. " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
  2831. " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
  2832. " fm.register(33,fm.fpioa.I2S0_WS)\n" +
  2833. "\n" +
  2834. " while True:\n" +
  2835. " _audio_play_state = _player.play()\n" +
  2836. " if (_audio_play_state) == True:\n" +
  2837. " print(\"Playing.\")\n" +
  2838. " elif (_audio_play_state) == False:\n" +
  2839. " print(\"Done.\")\n" +
  2840. " break\n" +
  2841. "\n";
  2842. // Blockly.Python.definitions_.import_audio = _in;
  2843. Blockly.Python.addVariable('_audio_play_state', _in, true);
  2844. // Blockly.Python.addSetup("ai_audio_read", _in);
  2845. var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
  2846. return _code;
  2847. };
  2848. Blockly.Blocks['ai_audio_play_state'] = {
  2849. init: function () {
  2850. this.appendDummyInput()
  2851. .appendField(Blockly.Msg.ai_audio_get_play_status);
  2852. this.setOutput(true, null);
  2853. this.setColour("#603ea0");
  2854. this.setTooltip("");
  2855. this.setHelpUrl("");
  2856. }
  2857. };
  2858. Blockly.Python['ai_audio_play_state'] = function (block) {
  2859. // TODO: Assemble Python into code variable.
  2860. var code = '_audio_play_state';
  2861. // TODO: Change ORDER_NONE to the correct strength.
  2862. return [code, Blockly.Python.ORDER_NONE];
  2863. };
  2864. Blockly.Blocks['ai_audio_play_finish'] = {
  2865. init: function () {
  2866. this.appendDummyInput()
  2867. .appendField(Blockly.Msg.ai_audio_end_play);
  2868. this.setPreviousStatement(true, null);
  2869. this.setNextStatement(true, null);
  2870. this.setColour("#603ea0");
  2871. this.setTooltip("");
  2872. this.setHelpUrl("");
  2873. }
  2874. };
  2875. Blockly.Python['ai_audio_play_finish'] = function (block) {
  2876. // TODO: Assemble Python into code variable.
  2877. var code = '_player.finish()\n';
  2878. return code;
  2879. };
  2880. Blockly.Blocks['ai_audio_set'] = {
  2881. init: function () {
  2882. this.appendValueInput("Num")
  2883. .setCheck(null)
  2884. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  2885. this.setInputsInline(true);
  2886. this.setPreviousStatement(true, null);
  2887. this.setNextStatement(true, null);
  2888. this.setColour("#603ea0");
  2889. this.setTooltip("");
  2890. this.setHelpUrl("");
  2891. }
  2892. };
  2893. Blockly.Python.ai_audio_set = function (block) {
  2894. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  2895. var _code = "_player.volume(" + _num + ")\n"
  2896. return _code;
  2897. }
  2898. var SPEECH_RECOGNITION_BLOCK_COLOR = "230";
  2899. Blockly.Blocks['speech_recognition_new_setup'] = {
  2900. init: function () {
  2901. this.appendDummyInput()
  2902. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
  2903. this.appendDummyInput()
  2904. .appendField(Blockly.Msg.ai_speech_recognition_setup);
  2905. this.appendDummyInput()
  2906. .appendField(Blockly.Msg.ai_speech_recognition_setup_set_sensi)
  2907. .appendField(new Blockly.FieldDropdown([
  2908. [Blockly.Msg.ai_speech_recognition_setup_set_sensi_normal, "10000"],
  2909. [Blockly.Msg.ai_speech_recognition_setup_set_sensi_high, "20000"],
  2910. [Blockly.Msg.ai_speech_recognition_setup_set_sensi_ehigh, "30000"]
  2911. ]), "sensitivity");
  2912. this.setPreviousStatement(true, null);
  2913. this.setNextStatement(true, null);
  2914. this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
  2915. this.setTooltip(Blockly.Msg.speech_recognition_new_setup_TOOLTIP);
  2916. this.setHelpUrl("");
  2917. }
  2918. };
  2919. Blockly.Python['speech_recognition_new_setup'] = function (block) {
  2920. var dropdown_sensitivity = block.getFieldValue('sensitivity');
  2921. Blockly.Python.definitions_.speech_recognition_new_setup_def = "" +
  2922. "import os, json, time, utime, struct\n" +
  2923. "from Maix import I2S, GPIO\n" +
  2924. "from modules import SpeechRecognizer\n" +
  2925. "from fpioa_manager import *\n" +
  2926. "fm.register(31, fm.fpioa.GPIO3)\n" +
  2927. "fm.register(32, fm.fpioa.GPIO4)\n" +
  2928. "\n" +
  2929. "_led_red = GPIO(GPIO.GPIO3, GPIO.OUT)\n" +
  2930. "_led_blue = GPIO(GPIO.GPIO4, GPIO.OUT)\n" +
  2931. "\n" +
  2932. "def _sr_data_save(s,content,keyword_num, model_num, path):\n" +
  2933. " data = _s_daemon.get_model_data(keyword_num, model_num)\n" +
  2934. " with open(path,'w') as f:\n" +
  2935. " f.write(data)\n" +
  2936. "\n" +
  2937. "def _sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
  2938. " print(path)\n" +
  2939. " with open(path,'r') as f:\n" +
  2940. " data = f.read()\n" +
  2941. " _s_daemon.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
  2942. "\n" +
  2943. "def _sr_init_remove_old_recording():\n" +
  2944. " global _voice_record\n" +
  2945. " try:\n" +
  2946. " for i in os.listdir(\"/sd/sr\"):\n" +
  2947. " print(\"deleting \" + str(i) + \"...\")\n" +
  2948. " os.remove(\"/sd/sr/\"+str(i))\n" +
  2949. " print(\"file deleting done.\")\n" +
  2950. " os.rmdir(\"/sd/sr\")\n" +
  2951. " print(\"directory deleting done.\")\n" +
  2952. " os.mkdir(\"/sd/sr\")\n" +
  2953. " print(\"directory creating done.\")\n" +
  2954. " except:\n" +
  2955. " os.mkdir(\"/sd/sr\")\n" +
  2956. " print(\"directory creating done.\")\n" +
  2957. " _voice_record = True\n" +
  2958. "\n" +
  2959. "# Enable Microphone and Disable Wifi Feature\n" +
  2960. "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
  2961. "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
  2962. "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
  2963. "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
  2964. "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
  2965. "wifi_en.value(0)\n" +
  2966. "\n" +
  2967. "# Init recording device parameteres\n" +
  2968. "sample_rate = 8000\n" +
  2969. "i2s_dev = I2S(I2S.DEVICE_0)\n" +
  2970. "# config i2s according to speechrecognizer\n" +
  2971. "i2s_dev.channel_config(i2s_dev.CHANNEL_0,I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT,cycles = I2S.SCLK_CYCLES_32,align_mode = I2S.STANDARD_MODE)\n" +
  2972. "i2s_dev.set_sample_rate(sample_rate)\n" +
  2973. "_s_daemon = SpeechRecognizer(i2s_dev)\n" +
  2974. "_s_daemon.set_threshold(0,0," + dropdown_sensitivity + ")\n" +
  2975. "";
  2976. var code = '';
  2977. return code;
  2978. };
  2979. Blockly.Blocks['speech_recognition_new_recording_sample_start'] = {
  2980. init: function () {
  2981. this.appendDummyInput()
  2982. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recording_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  2983. this.appendDummyInput()
  2984. .appendField(Blockly.Msg.ai_speech_recognition_start_recording);
  2985. this.appendDummyInput()
  2986. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_text)
  2987. .appendField(new Blockly.FieldNumber(2, 1, 6, 1), "clip")
  2988. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_note);
  2989. this.appendDummyInput()
  2990. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_text)
  2991. .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "take")
  2992. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_note);
  2993. this.appendDummyInput()
  2994. .appendField(Blockly.Msg.image_process_interface)
  2995. .appendField(new Blockly.FieldDropdown([
  2996. [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  2997. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  2998. [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  2999. ]), "interface")
  3000. this.setPreviousStatement(true, null);
  3001. this.setNextStatement(true, null);
  3002. this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
  3003. this.setTooltip(Blockly.Msg.speech_recognition_new_recording_sample_start_TOOLTIP);
  3004. this.setHelpUrl("");
  3005. }
  3006. };
  3007. Blockly.Python['speech_recognition_new_recording_sample_start'] = function (block) {
  3008. var number_clip = block.getFieldValue('clip');
  3009. var number_take = block.getFieldValue('take');
  3010. var interface = block.getFieldValue('interface');
  3011. var code = '' +
  3012. '# Remove old recordings\n' +
  3013. '_sr_init_remove_old_recording()\n' +
  3014. '\n' +
  3015. 'import lcd\n' +
  3016. 'import image\n' +
  3017. 'try:from cocorobo import display_cjk_string\n' +
  3018. 'except:pass\n' +
  3019. '\n' +
  3020. 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n' +
  3021. ' try:\n' +
  3022. ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n' +
  3023. ' return canvas\n' +
  3024. ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n' +
  3025. '\n' +
  3026. '_canvas_x, _canvas_y = 0, 0\n' +
  3027. 'lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n' +
  3028. 'lcd.rotation(1)\n' +
  3029. 'lcd.clear(lcd.BLACK)\n' +
  3030. 'canvas = image.Image(size=(240, 240))\n' +
  3031. 'canvas.clear()\n' +
  3032. 'canvas.draw_rectangle(0,0, 240, 240, color=(0,0,0), thickness=1, fill=True)\n';
  3033. if (interface == "English") {
  3034. code += 'lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
  3035. } else {
  3036. code += 'lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
  3037. }
  3038. code += '\n' +
  3039. '_voice_keyword_num = ' + number_clip + '\n' +
  3040. '_voice_model_num = ' + number_take + '\n' +
  3041. 'for k in range(_voice_keyword_num):\n' +
  3042. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3043. ' for l in range(_voice_model_num):\n' +
  3044. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3045. '\n' +
  3046. 'lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3047. '\n' +
  3048. 'from fpioa_manager import *\n' +
  3049. 'from Maix import FPIOA, GPIO\n' +
  3050. 'import time\n' +
  3051. 'from machine import Timer\n' +
  3052. '\n' +
  3053. '_gp_side_buttons = [9, 10, 11]\n' +
  3054. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  3055. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  3056. '_voice_button_status = False\n' +
  3057. '\n' +
  3058. 'def _on_read_voice_recog_button_timer(timer):\n' +
  3059. ' global _voice_button_status\n' +
  3060. ' if _gp_side_a.value() == True: _voice_button_status = True\n' +
  3061. ' elif _gp_side_a.value() == False: _voice_button_status = False\n' +
  3062. '\n' +
  3063. '_on_read_voice_recog_periodic_tim = Timer(Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PERIODIC, period=1, callback=_on_read_voice_recog_button_timer, arg=_on_read_voice_recog_button_timer)\n' +
  3064. '_on_read_voice_recog_periodic_tim.start()\n' +
  3065. '\n' +
  3066. 'try:\n' +
  3067. ' while _voice_record == True:\n' +
  3068. '\n' +
  3069. ' for i in range(_voice_keyword_num):\n' +
  3070. ' for j in range(_voice_model_num):\n' +
  3071. ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
  3072. '\n' +
  3073. ' while True:\n' +
  3074. ' if _voice_button_status == True:\n' +
  3075. ' break\n' +
  3076. ' else:\n' +
  3077. ' print(".", end="")\n' +
  3078. ' _led_red.value(1)\n' +
  3079. ' time.sleep_ms(100)\n' +
  3080. ' _led_red.value(0)\n' +
  3081. ' time.sleep_ms(100)\n' +
  3082. '\n' +
  3083. ' # Start recoding procedure\n' +
  3084. ' _led_red.value(0)\n' +
  3085. ' _s_daemon.record(i, j)\n' +
  3086. ' time.sleep_ms(500)\n' +
  3087. '\n' +
  3088. ' # Check recording state\n' +
  3089. ' while (_s_daemon.get_status() != 2):\n' +
  3090. ' if (_s_daemon.get_status() == 1):\n' +
  3091. ' print("[CocoRobo] 现在开始说!")\n' +
  3092. ' _led_red.value(1)\n' +
  3093. ' _led_blue.value(1)\n' +
  3094. ' canvas.clear()\n';
  3095. if (interface == "English") {
  3096. code += ' lcd_draw_string(canvas, 65,0, "Speak Now", color=(255,0,0), scale=2, mono_space=False)\n';
  3097. } else if (interface == 'Simplified Chinese') {
  3098. code += ' lcd_draw_string(canvas, 75,0, "现在开始说", font_size=1, color=(255,0,0))\n';
  3099. } else {
  3100. code += ' lcd_draw_string(canvas, 75,0, "現在開始說", font_size=1, color=(255,0,0))\n';
  3101. }
  3102. code += '\n' +
  3103. ' if (_s_daemon.get_status() == 5):\n' +
  3104. ' print("[CocoRobo] 收集噪音...")\n' +
  3105. ' _led_red.value(1)\n' +
  3106. ' canvas.clear()\n';
  3107. if (interface == "English") {
  3108. code += ' lcd_draw_string(canvas, 45,0, "Gathering Noise...", color=(255,0,0), scale=2, mono_space=False)\n';
  3109. } else {
  3110. code += ' lcd_draw_string(canvas, 75,0, "收集噪音...", font_size=1, color=(255,0,0))\n';
  3111. }
  3112. code += '\n' +
  3113. ' if i == 0:\n' +
  3114. ' if j == 0:\n' +
  3115. ' for k in range(_voice_keyword_num):\n' +
  3116. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3117. ' for l in range(_voice_model_num):\n' +
  3118. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3119. ' elif j == 1:\n' +
  3120. ' for k in range(_voice_keyword_num):\n' +
  3121. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3122. ' for l in range(_voice_model_num):\n' +
  3123. ' if k == 0 and l < 1:\n' +
  3124. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3125. ' else:\n' +
  3126. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3127. ' elif j == 2:\n' +
  3128. ' for k in range(_voice_keyword_num):\n' +
  3129. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3130. ' for l in range(_voice_model_num):\n' +
  3131. ' if k == 0 and l < 2:\n' +
  3132. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3133. ' else:\n' +
  3134. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3135. ' else:\n' +
  3136. ' for k in range(_voice_keyword_num):\n' +
  3137. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3138. ' for l in range(_voice_model_num):\n' +
  3139. ' if k == 0 and l < 3:\n' +
  3140. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3141. ' else:\n' +
  3142. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3143. '\n' +
  3144. ' elif i == 1:\n' +
  3145. ' if j == 0:\n' +
  3146. ' for k in range(_voice_keyword_num):\n' +
  3147. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3148. ' for l in range(_voice_model_num):\n' +
  3149. ' if k < 1:\n' +
  3150. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3151. ' else:\n' +
  3152. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3153. ' elif j == 1:\n' +
  3154. ' for k in range(_voice_keyword_num):\n' +
  3155. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3156. ' for l in range(_voice_model_num):\n' +
  3157. ' if k < 1 or (k == 1 and l < 1):\n' +
  3158. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3159. ' else:\n' +
  3160. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3161. ' elif j == 2:\n' +
  3162. ' for k in range(_voice_keyword_num):\n' +
  3163. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3164. ' for l in range(_voice_model_num):\n' +
  3165. ' if k < 1 or (k == 1 and l < 2):\n' +
  3166. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3167. ' else:\n' +
  3168. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3169. ' else:\n' +
  3170. ' for k in range(_voice_keyword_num):\n' +
  3171. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3172. ' for l in range(_voice_model_num):\n' +
  3173. ' if k < 1 or (k == 1 and l < 3):\n' +
  3174. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3175. ' else:\n' +
  3176. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3177. '\n' +
  3178. ' elif i == 2:\n' +
  3179. ' if j == 0:\n' +
  3180. ' for k in range(_voice_keyword_num):\n' +
  3181. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3182. ' for l in range(_voice_model_num):\n' +
  3183. ' if k < 2:\n' +
  3184. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3185. ' else:\n' +
  3186. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3187. ' elif j == 1:\n' +
  3188. ' for k in range(_voice_keyword_num):\n' +
  3189. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3190. ' for l in range(_voice_model_num):\n' +
  3191. ' if k < 2 or (k == 2 and l < 1):\n' +
  3192. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3193. ' else:\n' +
  3194. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3195. ' elif j == 2:\n' +
  3196. ' for k in range(_voice_keyword_num):\n' +
  3197. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3198. ' for l in range(_voice_model_num):\n' +
  3199. ' if k < 2 or (k == 2 and l < 2):\n' +
  3200. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3201. ' else:\n' +
  3202. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3203. ' else:\n' +
  3204. ' for k in range(_voice_keyword_num):\n' +
  3205. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3206. ' for l in range(_voice_model_num):\n' +
  3207. ' if k < 2 or (k == 2 and l < 3):\n' +
  3208. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3209. ' else:\n' +
  3210. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3211. '\n' +
  3212. ' elif i == 3:\n' +
  3213. ' if j == 0:\n' +
  3214. ' for k in range(_voice_keyword_num):\n' +
  3215. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3216. ' for l in range(_voice_model_num):\n' +
  3217. ' if k < 3:\n' +
  3218. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3219. ' else:\n' +
  3220. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3221. ' elif j == 1:\n' +
  3222. ' for k in range(_voice_keyword_num):\n' +
  3223. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3224. ' for l in range(_voice_model_num):\n' +
  3225. ' if k < 3 or (k == 3 and l < 1):\n' +
  3226. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3227. ' else:\n' +
  3228. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3229. ' elif j == 2:\n' +
  3230. ' for k in range(_voice_keyword_num):\n' +
  3231. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3232. ' for l in range(_voice_model_num):\n' +
  3233. ' if k < 3 or (k == 3 and l < 2):\n' +
  3234. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3235. ' else:\n' +
  3236. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3237. ' else:\n' +
  3238. ' for k in range(_voice_keyword_num):\n' +
  3239. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3240. ' for l in range(_voice_model_num):\n' +
  3241. ' if k < 3 or (k == 3 and l < 3):\n' +
  3242. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3243. ' else:\n' +
  3244. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3245. '\n' +
  3246. ' elif i == 4:\n' +
  3247. ' if j == 0:\n' +
  3248. ' for k in range(_voice_keyword_num):\n' +
  3249. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3250. ' for l in range(_voice_model_num):\n' +
  3251. ' if k < 4:\n' +
  3252. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3253. ' else:\n' +
  3254. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3255. ' elif j == 1:\n' +
  3256. ' for k in range(_voice_keyword_num):\n' +
  3257. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3258. ' for l in range(_voice_model_num):\n' +
  3259. ' if k < 4 or (k == 4 and l < 1):\n' +
  3260. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3261. ' else:\n' +
  3262. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3263. ' elif j == 2:\n' +
  3264. ' for k in range(_voice_keyword_num):\n' +
  3265. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3266. ' for l in range(_voice_model_num):\n' +
  3267. ' if k < 4 or (k == 4 and l < 2):\n' +
  3268. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3269. ' else:\n' +
  3270. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3271. ' else:\n' +
  3272. ' for k in range(_voice_keyword_num):\n' +
  3273. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3274. ' for l in range(_voice_model_num):\n' +
  3275. ' if k < 4 or (k == 4 and l < 3):\n' +
  3276. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3277. ' else:\n' +
  3278. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3279. '\n' +
  3280. ' else:\n' +
  3281. ' if j == 0:\n' +
  3282. ' for k in range(_voice_keyword_num):\n' +
  3283. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3284. ' for l in range(_voice_model_num):\n' +
  3285. ' if k < 5:\n' +
  3286. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3287. ' else:\n' +
  3288. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3289. ' elif j == 1:\n' +
  3290. ' for k in range(_voice_keyword_num):\n' +
  3291. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3292. ' for l in range(_voice_model_num):\n' +
  3293. ' if k < 5 or (k == 5 and l < 1):\n' +
  3294. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3295. ' else:\n' +
  3296. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3297. ' elif j == 2:\n' +
  3298. ' for k in range(_voice_keyword_num):\n' +
  3299. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3300. ' for l in range(_voice_model_num):\n' +
  3301. ' if k < 5 or (k == 5 and l < 2):\n' +
  3302. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3303. ' else:\n' +
  3304. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3305. ' else:\n' +
  3306. ' for k in range(_voice_keyword_num):\n' +
  3307. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3308. ' for l in range(_voice_model_num):\n' +
  3309. ' if k < 5 or (k == 5 and l < 3):\n' +
  3310. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3311. ' else:\n' +
  3312. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3313. '\n' +
  3314. ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3315. ' time.sleep_ms(500)\n' +
  3316. ' if (_s_daemon.get_status() == 2):\n' +
  3317. ' canvas.clear()\n';
  3318. if (interface == "English") {
  3319. code += ' lcd_draw_string(canvas, 95,0, "OK", color=(10,255,0), scale=2, mono_space=False)\n';
  3320. } else {
  3321. code += ' lcd_draw_string(canvas, 95,0, "成功 ", font_size=1, color=(10,255,0))\n';
  3322. }
  3323. code += '\n' +
  3324. ' if i == 0:\n' +
  3325. ' if j == 0:\n' +
  3326. ' for k in range(_voice_keyword_num):\n' +
  3327. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3328. ' for l in range(_voice_model_num):\n' +
  3329. ' if k == 0 and l < 1:\n' +
  3330. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3331. ' else:\n' +
  3332. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3333. ' elif j == 1:\n' +
  3334. ' for k in range(_voice_keyword_num):\n' +
  3335. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3336. ' for l in range(_voice_model_num):\n' +
  3337. ' if k == 0 and l < 2:\n' +
  3338. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3339. ' else:\n' +
  3340. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3341. ' elif j == 2:\n' +
  3342. ' for k in range(_voice_keyword_num):\n' +
  3343. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3344. ' for l in range(_voice_model_num):\n' +
  3345. ' if k == 0 and l < 3:\n' +
  3346. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3347. ' else:\n' +
  3348. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3349. ' else:\n' +
  3350. ' for k in range(_voice_keyword_num):\n' +
  3351. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3352. ' for l in range(_voice_model_num):\n' +
  3353. ' if k == 0 and l < 4:\n' +
  3354. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3355. ' else:\n' +
  3356. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3357. '\n' +
  3358. ' elif i == 1:\n' +
  3359. ' if j == 0:\n' +
  3360. ' for k in range(_voice_keyword_num):\n' +
  3361. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3362. ' for l in range(_voice_model_num):\n' +
  3363. ' if k < 1 or (k == 1 and l < 1):\n' +
  3364. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3365. ' else:\n' +
  3366. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3367. ' elif j == 1:\n' +
  3368. ' for k in range(_voice_keyword_num):\n' +
  3369. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3370. ' for l in range(_voice_model_num):\n' +
  3371. ' if k < 1 or (k == 1 and l < 2):\n' +
  3372. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3373. ' else:\n' +
  3374. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3375. ' elif j == 2:\n' +
  3376. ' for k in range(_voice_keyword_num):\n' +
  3377. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3378. ' for l in range(_voice_model_num):\n' +
  3379. ' if k < 1 or (k == 1 and l < 3):\n' +
  3380. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3381. ' else:\n' +
  3382. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3383. ' else:\n' +
  3384. ' for k in range(_voice_keyword_num):\n' +
  3385. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3386. ' for l in range(_voice_model_num):\n' +
  3387. ' if k < 1 or (k == 1 and l < 4):\n' +
  3388. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3389. ' else:\n' +
  3390. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3391. '\n' +
  3392. ' elif i == 2:\n' +
  3393. ' if j == 0:\n' +
  3394. ' for k in range(_voice_keyword_num):\n' +
  3395. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3396. ' for l in range(_voice_model_num):\n' +
  3397. ' if k < 2 or (k == 2 and l < 1):\n' +
  3398. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3399. ' else:\n' +
  3400. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3401. ' elif j == 1:\n' +
  3402. ' for k in range(_voice_keyword_num):\n' +
  3403. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3404. ' for l in range(_voice_model_num):\n' +
  3405. ' if k < 2 or (k == 2 and l < 2):\n' +
  3406. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3407. ' else:\n' +
  3408. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3409. ' elif j == 2:\n' +
  3410. ' for k in range(_voice_keyword_num):\n' +
  3411. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3412. ' for l in range(_voice_model_num):\n' +
  3413. ' if k < 2 or (k == 2 and l < 3):\n' +
  3414. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3415. ' else:\n' +
  3416. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3417. ' else:\n' +
  3418. ' for k in range(_voice_keyword_num):\n' +
  3419. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3420. ' for l in range(_voice_model_num):\n' +
  3421. ' if k < 2 or (k == 2 and l < 4):\n' +
  3422. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3423. ' else:\n' +
  3424. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3425. '\n' +
  3426. ' elif i == 3:\n' +
  3427. ' if j == 0:\n' +
  3428. ' for k in range(_voice_keyword_num):\n' +
  3429. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3430. ' for l in range(_voice_model_num):\n' +
  3431. ' if k < 3 or (k == 3 and l < 1):\n' +
  3432. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3433. ' else:\n' +
  3434. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3435. ' elif j == 1:\n' +
  3436. ' for k in range(_voice_keyword_num):\n' +
  3437. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3438. ' for l in range(_voice_model_num):\n' +
  3439. ' if k < 3 or (k == 3 and l < 2):\n' +
  3440. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3441. ' else:\n' +
  3442. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3443. ' elif j == 2:\n' +
  3444. ' for k in range(_voice_keyword_num):\n' +
  3445. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3446. ' for l in range(_voice_model_num):\n' +
  3447. ' if k < 3 or (k == 3 and l < 3):\n' +
  3448. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3449. ' else:\n' +
  3450. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3451. ' else:\n' +
  3452. ' for k in range(_voice_keyword_num):\n' +
  3453. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3454. ' for l in range(_voice_model_num):\n' +
  3455. ' if k < 3 or (k == 3 and l < 4):\n' +
  3456. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3457. ' else:\n' +
  3458. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3459. '\n' +
  3460. ' elif i == 4:\n' +
  3461. ' if j == 0:\n' +
  3462. ' for k in range(_voice_keyword_num):\n' +
  3463. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3464. ' for l in range(_voice_model_num):\n' +
  3465. ' if k < 4 or (k == 4 and l < 1):\n' +
  3466. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3467. ' else:\n' +
  3468. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3469. ' elif j == 1:\n' +
  3470. ' for k in range(_voice_keyword_num):\n' +
  3471. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3472. ' for l in range(_voice_model_num):\n' +
  3473. ' if k < 4 or (k == 4 and l < 2):\n' +
  3474. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3475. ' else:\n' +
  3476. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3477. ' elif j == 2:\n' +
  3478. ' for k in range(_voice_keyword_num):\n' +
  3479. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3480. ' for l in range(_voice_model_num):\n' +
  3481. ' if k < 4 or (k == 4 and l < 3):\n' +
  3482. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3483. ' else:\n' +
  3484. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3485. ' else:\n' +
  3486. ' for k in range(_voice_keyword_num):\n' +
  3487. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3488. ' for l in range(_voice_model_num):\n' +
  3489. ' if k < 4 or (k == 4 and l < 4):\n' +
  3490. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3491. ' else:\n' +
  3492. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3493. '\n' +
  3494. ' else:\n' +
  3495. ' if j == 0:\n' +
  3496. ' for k in range(_voice_keyword_num):\n' +
  3497. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3498. ' for l in range(_voice_model_num):\n' +
  3499. ' if k < 5 or (k == 5 and l < 1):\n' +
  3500. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3501. ' else:\n' +
  3502. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3503. ' elif j == 1:\n' +
  3504. ' for k in range(_voice_keyword_num):\n' +
  3505. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3506. ' for l in range(_voice_model_num):\n' +
  3507. ' if k < 5 or (k == 5 and l < 2):\n' +
  3508. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3509. ' else:\n' +
  3510. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3511. ' elif j == 2:\n' +
  3512. ' for k in range(_voice_keyword_num):\n' +
  3513. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3514. ' for l in range(_voice_model_num):\n' +
  3515. ' if k < 5 or (k == 5 and l < 3):\n' +
  3516. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3517. ' else:\n' +
  3518. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3519. ' else:\n' +
  3520. ' for k in range(_voice_keyword_num):\n' +
  3521. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3522. ' for l in range(_voice_model_num):\n' +
  3523. ' if k < 5 or (k == 5 and l < 4):\n' +
  3524. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3525. ' else:\n' +
  3526. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3527. '\n' +
  3528. ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3529. ' for t in range(0,6,1):\n' +
  3530. ' _led_blue.value(1)\n' +
  3531. ' _led_red.value(1)\n' +
  3532. ' time.sleep_ms(100)\n' +
  3533. ' _led_blue.value(0)\n' +
  3534. ' _led_red.value(0)\n' +
  3535. ' time.sleep_ms(100)\n' +
  3536. ' time.sleep(1)\n' +
  3537. '\n' +
  3538. ' canvas.clear()\n';
  3539. if (interface == "English") {
  3540. code += ' lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
  3541. } else {
  3542. code += ' lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
  3543. }
  3544. code += '\n' +
  3545. ' if i == 0:\n' +
  3546. ' if j == 0:\n' +
  3547. ' for k in range(_voice_keyword_num):\n' +
  3548. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3549. ' for l in range(_voice_model_num):\n' +
  3550. ' if k == 0 and l < 1:\n' +
  3551. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3552. ' else:\n' +
  3553. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3554. ' elif j == 1:\n' +
  3555. ' for k in range(_voice_keyword_num):\n' +
  3556. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3557. ' for l in range(_voice_model_num):\n' +
  3558. ' if k == 0 and l < 2:\n' +
  3559. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3560. ' else:\n' +
  3561. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3562. ' elif j == 2:\n' +
  3563. ' for k in range(_voice_keyword_num):\n' +
  3564. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3565. ' for l in range(_voice_model_num):\n' +
  3566. ' if k == 0 and l < 3:\n' +
  3567. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3568. ' else:\n' +
  3569. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3570. ' else:\n' +
  3571. ' for k in range(_voice_keyword_num):\n' +
  3572. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3573. ' for l in range(_voice_model_num):\n' +
  3574. ' if k == 0 and l < 4:\n' +
  3575. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3576. ' else:\n' +
  3577. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3578. '\n' +
  3579. ' elif i == 1:\n' +
  3580. ' if j == 0:\n' +
  3581. ' for k in range(_voice_keyword_num):\n' +
  3582. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3583. ' for l in range(_voice_model_num):\n' +
  3584. ' if k < 1 or (k == 1 and l < 1):\n' +
  3585. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3586. ' else:\n' +
  3587. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3588. ' elif j == 1:\n' +
  3589. ' for k in range(_voice_keyword_num):\n' +
  3590. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3591. ' for l in range(_voice_model_num):\n' +
  3592. ' if k < 1 or (k == 1 and l < 2):\n' +
  3593. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3594. ' else:\n' +
  3595. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3596. ' elif j == 2:\n' +
  3597. ' for k in range(_voice_keyword_num):\n' +
  3598. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3599. ' for l in range(_voice_model_num):\n' +
  3600. ' if k < 1 or (k == 1 and l < 3):\n' +
  3601. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3602. ' else:\n' +
  3603. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3604. ' else:\n' +
  3605. ' for k in range(_voice_keyword_num):\n' +
  3606. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3607. ' for l in range(_voice_model_num):\n' +
  3608. ' if k < 1 or (k == 1 and l < 4):\n' +
  3609. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3610. ' else:\n' +
  3611. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3612. '\n' +
  3613. ' elif i == 2:\n' +
  3614. ' if j == 0:\n' +
  3615. ' for k in range(_voice_keyword_num):\n' +
  3616. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3617. ' for l in range(_voice_model_num):\n' +
  3618. ' if k < 2 or (k == 2 and l < 1):\n' +
  3619. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3620. ' else:\n' +
  3621. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3622. ' elif j == 1:\n' +
  3623. ' for k in range(_voice_keyword_num):\n' +
  3624. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3625. ' for l in range(_voice_model_num):\n' +
  3626. ' if k < 2 or (k == 2 and l < 2):\n' +
  3627. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3628. ' else:\n' +
  3629. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3630. ' elif j == 2:\n' +
  3631. ' for k in range(_voice_keyword_num):\n' +
  3632. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3633. ' for l in range(_voice_model_num):\n' +
  3634. ' if k < 2 or (k == 2 and l < 3):\n' +
  3635. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3636. ' else:\n' +
  3637. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3638. ' else:\n' +
  3639. ' for k in range(_voice_keyword_num):\n' +
  3640. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3641. ' for l in range(_voice_model_num):\n' +
  3642. ' if k < 2 or (k == 2 and l < 4):\n' +
  3643. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3644. ' else:\n' +
  3645. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3646. '\n' +
  3647. ' elif i == 3:\n' +
  3648. ' if j == 0:\n' +
  3649. ' for k in range(_voice_keyword_num):\n' +
  3650. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3651. ' for l in range(_voice_model_num):\n' +
  3652. ' if k < 3 or (k == 3 and l < 1):\n' +
  3653. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3654. ' else:\n' +
  3655. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3656. ' elif j == 1:\n' +
  3657. ' for k in range(_voice_keyword_num):\n' +
  3658. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3659. ' for l in range(_voice_model_num):\n' +
  3660. ' if k < 3 or (k == 3 and l < 2):\n' +
  3661. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3662. ' else:\n' +
  3663. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3664. ' elif j == 2:\n' +
  3665. ' for k in range(_voice_keyword_num):\n' +
  3666. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3667. ' for l in range(_voice_model_num):\n' +
  3668. ' if k < 3 or (k == 3 and l < 3):\n' +
  3669. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3670. ' else:\n' +
  3671. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3672. ' else:\n' +
  3673. ' for k in range(_voice_keyword_num):\n' +
  3674. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3675. ' for l in range(_voice_model_num):\n' +
  3676. ' if k < 3 or (k == 3 and l < 4):\n' +
  3677. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3678. ' else:\n' +
  3679. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3680. '\n' +
  3681. ' elif i == 4:\n' +
  3682. ' if j == 0:\n' +
  3683. ' for k in range(_voice_keyword_num):\n' +
  3684. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3685. ' for l in range(_voice_model_num):\n' +
  3686. ' if k < 4 or (k == 4 and l < 1):\n' +
  3687. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3688. ' else:\n' +
  3689. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3690. ' elif j == 1:\n' +
  3691. ' for k in range(_voice_keyword_num):\n' +
  3692. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3693. ' for l in range(_voice_model_num):\n' +
  3694. ' if k < 4 or (k == 4 and l < 2):\n' +
  3695. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3696. ' else:\n' +
  3697. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3698. ' elif j == 2:\n' +
  3699. ' for k in range(_voice_keyword_num):\n' +
  3700. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3701. ' for l in range(_voice_model_num):\n' +
  3702. ' if k < 4 or (k == 4 and l < 3):\n' +
  3703. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3704. ' else:\n' +
  3705. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3706. ' else:\n' +
  3707. ' for k in range(_voice_keyword_num):\n' +
  3708. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3709. ' for l in range(_voice_model_num):\n' +
  3710. ' if k < 4 or (k == 4 and l < 4):\n' +
  3711. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3712. ' else:\n' +
  3713. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3714. '\n' +
  3715. ' else:\n' +
  3716. ' if j == 0:\n' +
  3717. ' for k in range(_voice_keyword_num):\n' +
  3718. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3719. ' for l in range(_voice_model_num):\n' +
  3720. ' if k < 5 or (k == 5 and l < 1):\n' +
  3721. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3722. ' else:\n' +
  3723. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3724. ' elif j == 1:\n' +
  3725. ' for k in range(_voice_keyword_num):\n' +
  3726. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3727. ' for l in range(_voice_model_num):\n' +
  3728. ' if k < 5 or (k == 5 and l < 2):\n' +
  3729. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3730. ' else:\n' +
  3731. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3732. ' elif j == 2:\n' +
  3733. ' for k in range(_voice_keyword_num):\n' +
  3734. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3735. ' for l in range(_voice_model_num):\n' +
  3736. ' if k < 5 or (k == 5 and l < 3):\n' +
  3737. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3738. ' else:\n' +
  3739. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3740. ' else:\n' +
  3741. ' for k in range(_voice_keyword_num):\n' +
  3742. ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
  3743. ' for l in range(_voice_model_num):\n' +
  3744. ' if k < 5 or (k == 5 and l < 4):\n' +
  3745. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
  3746. ' else:\n' +
  3747. ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
  3748. '\n' +
  3749. ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3750. '\n' +
  3751. ' # Done Recording\n' +
  3752. ' content = str(i) + \'_\' + str(j)\n' +
  3753. ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+"_" + str(_s_daemon.get_model_info(i,j)).strip("[]") +".sr"\n' +
  3754. ' _sr_data_save(_s_daemon, content, i, j, file_name)\n' +
  3755. ' print("frm_num is: " + str(_s_daemon.get_model_info(i,j)))\n' +
  3756. '\n' +
  3757. ' print("Record successful! Next, Run your speech recognition code!")\n' +
  3758. ' _voice_record = False\n' +
  3759. '\n' +
  3760. ' canvas.clear()\n' +
  3761. ' canvas.draw_image(image.Image("/sd/preset/images/tick.jpg"), 70,70, x_scale=0.5, y_scale=0.5)\n' +
  3762. ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3763. ' _led_blue.value(1)\n' +
  3764. ' _led_red.value(0)\n' +
  3765. ' time.sleep(2)\n' +
  3766. 'except BaseException as e:\n' +
  3767. ' print(str(e))\n' +
  3768. ' canvas.clear()\n';
  3769. if (interface == "English") {
  3770. code += ' lcd_draw_string(canvas,30,40, "Voice Sample", color=(255,0,0), scale=1, mono_space=False)\n' +
  3771. ' lcd_draw_string(canvas,30,70, "Recording", color=(255,0,0), scale=1, mono_space=False)\n' +
  3772. ' lcd_draw_string(canvas,30,100, "Failed!", color=(255,0,0), scale=1, mono_space=False)\n' +
  3773. ' lcd_draw_string(canvas,30,140, "Reason: " + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
  3774. } else if (interface == 'Simplified Chinese') {
  3775. code += ' lcd_draw_string(canvas,30,40, "语音样本",font_size=1, color=(255,0,0))\n' +
  3776. ' lcd_draw_string(canvas,30,70, "正在记录中",font_size=1, color=(255,0,0))\n' +
  3777. ' lcd_draw_string(canvas,30,100, "失败!",font_size=1, color=(255,0,0))\n' +
  3778. ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
  3779. } else {
  3780. code += ' lcd_draw_string(canvas,30,40, "語音樣本",font_size=1, color=(255,0,0))\n' +
  3781. ' lcd_draw_string(canvas,30,70, "正在記錄中",font_size=1, color=(255,0,0))\n' +
  3782. ' lcd_draw_string(canvas,30,100, "失敗!",font_size=1, color=(255,0,0))\n' +
  3783. ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
  3784. }
  3785. code += '' +
  3786. ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
  3787. ' _led_blue.value(0)\n' +
  3788. ' _led_red.value(1)\n' +
  3789. '\n' +
  3790. 'import machine\n' +
  3791. 'machine.reset()\n' +
  3792. '';
  3793. return code;
  3794. };
  3795. Blockly.Blocks['speech_recognition_new_start_recognition'] = {
  3796. init: function () {
  3797. this.appendDummyInput()
  3798. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  3799. this.appendDummyInput()
  3800. .appendField(Blockly.Msg.ai_speech_recognition_start_recognizing);
  3801. this.setPreviousStatement(true, null);
  3802. this.setNextStatement(true, null);
  3803. this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
  3804. this.setTooltip(Blockly.Msg.speech_recognition_new_start_recognition_TOOLTIP);
  3805. this.setHelpUrl("");
  3806. }
  3807. };
  3808. Blockly.Python['speech_recognition_new_start_recognition'] = function (block) {
  3809. Blockly.Python.definitions_.speech_recognition_new_start_recognition_def = "" +
  3810. "import time\n\n" +
  3811. "_voice_recording_load = True\n" +
  3812. "_voice_recording_ready = False\n" +
  3813. "\n" +
  3814. "if _voice_recording_load == True:\n" +
  3815. " _sr_filelist = os.listdir(\"/sd/sr\")\n" +
  3816. " for _sr_file in _sr_filelist: \n" +
  3817. " _sr_frm_num = int(_sr_file[4:_sr_file.find(\".\")])\n" +
  3818. " print(_sr_frm_num)\n" +
  3819. " print(\"/sd/sr/\" + str(_sr_file))\n" +
  3820. " _sr_data_load(_s_daemon, int(_sr_file[0]), int(_sr_file[2]), _sr_frm_num, \"/sd/sr/\" + str(_sr_file))\n" +
  3821. " print(\"load successful!\")\n" +
  3822. "";
  3823. var code = '_s_recognition_state = 0\n' +
  3824. '_s_daemon.recognize()\n' +
  3825. 'time.sleep_ms(500)\n' +
  3826. '\n';
  3827. return code;
  3828. };
  3829. Blockly.Blocks['speech_recognition_new_recognition_get_any_result'] = {
  3830. init: function () {
  3831. this.appendDummyInput()
  3832. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text);
  3833. this.appendStatementInput("NAME")
  3834. .setCheck(null)
  3835. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  3836. this.setPreviousStatement(true, null);
  3837. this.setNextStatement(true, null);
  3838. this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
  3839. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_any_result_TOOLTIP);
  3840. this.setHelpUrl("");
  3841. }
  3842. };
  3843. Blockly.Python['speech_recognition_new_recognition_get_any_result'] = function (block) {
  3844. var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  3845. // TODO: Assemble Python into code variable.
  3846. var code = '' +
  3847. 'while _s_recognition_state == 0:\n' +
  3848. ' if (_s_daemon.get_status() == 5):\n' +
  3849. ' print("", end="")\n' +
  3850. ' elif (_s_daemon.get_status() == 3):\n' +
  3851. ' print("", end="")\n' +
  3852. ' elif (_s_daemon.get_status() == 4):\n' +
  3853. ' print("[CocoRobo] Record OK, Proceed!")\n' +
  3854. ' _s_recognition_state = 1\n' +
  3855. ' else:\n' +
  3856. ' print("[CocoRobo] Current state: " + str(s.get_status()))\n' +
  3857. '\n' +
  3858. 'time.sleep_ms(800)\n' +
  3859. '_s_ret = _s_daemon.get_result()\n' +
  3860. 'print("[CocoRobo] Result: " + str(_s_ret))\n' +
  3861. '\n' +
  3862. 'if (_s_ret > 0):\n' +
  3863. statements_name +
  3864. '';
  3865. return code;
  3866. };
  3867. Blockly.Blocks['speech_recognition_new_recognition_get_result'] = {
  3868. init: function () {
  3869. this.appendDummyInput()
  3870. .appendField(Blockly.Msg.ai_speech_recognition_get_result)
  3871. .appendField(new Blockly.FieldNumber(1, 1, 6, 1), "index")
  3872. .appendField(Blockly.Msg.ai_speech_recognition_get_result_after);
  3873. this.setOutput(true, null);
  3874. this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
  3875. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_result_TOOLTIP);
  3876. this.setHelpUrl("");
  3877. }
  3878. };
  3879. Blockly.Python['speech_recognition_new_recognition_get_result'] = function (block) {
  3880. var number_index = block.getFieldValue('index');
  3881. // TODO: Assemble Python into code variable.
  3882. var code = '(_s_ret == ' + number_index + ')';
  3883. // TODO: Change ORDER_NONE to the correct strength.
  3884. return [code, Blockly.Python.ORDER_NONE];
  3885. };
  3886. /*
  3887. Blockly.Blocks['ai_audio_voice_recognition_setup'] = {
  3888. init: function() {
  3889. this.appendDummyInput()
  3890. .appendField(new Blockly.FieldImage("blockly/media/voice_recognition_header.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
  3891. this.appendDummyInput()
  3892. .appendField("Voice Recognition Setup");
  3893. this.appendDummyInput()
  3894. .appendField("Set the Total Number of Voice Clip(s): ")
  3895. .appendField(new Blockly.FieldNumber(3, 1, 6, 1), "word_count")
  3896. .appendField(" (No more than 6)");
  3897. this.appendDummyInput()
  3898. .appendField("Set the Total Count of Sample(s) for each clip: ")
  3899. .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "repeat_count")
  3900. .appendField(" (No more than 4)");
  3901. this.setPreviousStatement(true, null);
  3902. this.setNextStatement(true, null);
  3903. this.setColour(230);
  3904. this.setTooltip("");
  3905. this.setHelpUrl("");
  3906. }
  3907. };
  3908. var global_voice_recognition_word_count = 0;
  3909. var global_voice_recognition_sample_count = 0;
  3910. Blockly.Python['ai_audio_voice_recognition_setup'] = function(block) {
  3911. var number_word_count = block.getFieldValue('word_count');
  3912. var number_repeat_count = block.getFieldValue('repeat_count');
  3913. // TODO: Assemble Python into code variable.
  3914. global_voice_recognition_word_count = number_word_count;
  3915. global_voice_recognition_sample_count = number_repeat_count;
  3916. console.log(global_voice_recognition_word_count + "," + global_voice_recognition_sample_count);
  3917. Blockly.Python.definitions_.voice_recognition_def = "" +
  3918. "import lcd\n" +
  3919. "import image\n" +
  3920. "\n" +
  3921. "lcd.init(type=1,freq=15000000,width=240,height=240,color=(0,0,0))\n" +
  3922. "lcd.rotation(1)\n" +
  3923. "lcd.clear(lcd.BLACK)\n" +
  3924. "\n" +
  3925. "import os, json, time, utime, struct\n" +
  3926. "from Maix import I2S, GPIO\n" +
  3927. "from modules import SpeechRecognizer\n" +
  3928. "from fpioa_manager import *\n" +
  3929. "\n" +
  3930. "fm.register(31, fm.fpioa.GPIO3)\n" +
  3931. "fm.register(32, fm.fpioa.GPIO4)\n" +
  3932. "\n" +
  3933. "led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n" +
  3934. "led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n" +
  3935. "\n" +
  3936. "def sr_data_save(s,content,keyword_num, model_num, path):\n" +
  3937. " data = s.get_model_data(keyword_num, model_num)\n" +
  3938. " with open(path,'w') as f:\n" +
  3939. " f.write(data)\n" +
  3940. "\n" +
  3941. "def sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
  3942. " print(path)\n" +
  3943. " with open(path,'r') as f:\n" +
  3944. " data = f.read()\n" +
  3945. " # print(data)\n" +
  3946. " s.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
  3947. "\n" +
  3948. "def sr_led_blinking(type):\n" +
  3949. " if type == \"waitfor\":\n" +
  3950. " led_red.value(1)\n" +
  3951. " time.sleep_ms(100)\n" +
  3952. " led_red.value(0)\n" +
  3953. " time.sleep_ms(100)\n" +
  3954. " elif type == \"recorded\":\n" +
  3955. " for i in range(0,5,1):\n" +
  3956. " led_blue.value(1)\n" +
  3957. " time.sleep_ms(100)\n" +
  3958. " led_blue.value(0)\n" +
  3959. " time.sleep_ms(100)\n" +
  3960. "\n" +
  3961. "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
  3962. "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
  3963. "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
  3964. "\n" +
  3965. "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
  3966. "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
  3967. "wifi_en.value(0)\n" +
  3968. "\n" +
  3969. "sample_rate = 8000\n" +
  3970. "i2s_dev = I2S(I2S.DEVICE_0)\n" +
  3971. "\n" +
  3972. "# config i2s according to speechrecognizer\n" +
  3973. "i2s_dev.channel_config(i2s_dev.CHANNEL_0,I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT,cycles = I2S.SCLK_CYCLES_32,align_mode = I2S.STANDARD_MODE)\n" +
  3974. "i2s_dev.set_sample_rate(sample_rate)\n" +
  3975. "s = SpeechRecognizer(i2s_dev)\n" +
  3976. "try:\n" +
  3977. " print(\"Setting get_noise_threshold to less sensitive.\")\n" +
  3978. " s.set_threshold(0,0,20000)\n" +
  3979. "except BaseException as e:\n" +
  3980. " print(\"Failed to set threshold, please check if your firmware is right, error info:\")\n" +
  3981. " print(str(e))\n" +
  3982. "";
  3983. var code = '';
  3984. return code;
  3985. };
  3986. Blockly.Blocks['ai_audio_voice_recognition_start_record'] = {
  3987. init: function() {
  3988. this.appendDummyInput()
  3989. .appendField("Start Recording Samples (By Pressing Button ")
  3990. .appendField(new Blockly.FieldDropdown([
  3991. ["A", "a"],
  3992. ["B", "b"],
  3993. ["C", "c"]
  3994. ]), "button_type")
  3995. .appendField(")");
  3996. this.appendDummyInput()
  3997. .appendField("Note 1: Recorded Samples Will Stored in SD Card.");
  3998. this.appendDummyInput()
  3999. .appendField("Note 2: Write down the frm_num for each sample.");
  4000. this.setPreviousStatement(true, null);
  4001. this.setNextStatement(true, null);
  4002. this.setColour(230);
  4003. this.setTooltip("");
  4004. this.setHelpUrl("");
  4005. }
  4006. };
  4007. Blockly.Python['ai_audio_voice_recognition_start_record'] = function(block) {
  4008. var dropdown_button_type = block.getFieldValue('button_type');
  4009. // TODO: Assemble Python into code variable.
  4010. Blockly.Python.addVariable('_voice_on_recoding_state', '', true);
  4011. Blockly.Python.definitions_.voice_recognition_start_record_def = "" +
  4012. "try:\n" +
  4013. " for i in os.listdir(\"/sd/sr\"):\n" +
  4014. " print(\"deleting \" + str(i) + \"...\")\n" +
  4015. " os.remove(\"/sd/sr/\"+str(i))\n" +
  4016. " print(\"file deleting done.\")\n" +
  4017. " os.rmdir(\"/sd/sr\")\n" +
  4018. " print(\"directory deleting done.\")\n" +
  4019. " os.mkdir(\"/sd/sr\")\n" +
  4020. " print(\"directory creating done.\")\n" +
  4021. "except:\n" +
  4022. " os.mkdir(\"/sd/sr\")\n" +
  4023. " print(\"directory creating done.\")\n" +
  4024. "\n" +
  4025. "_voice_record = True\n" +
  4026. "\n" +
  4027. "from fpioa_manager import *\n" +
  4028. "from Maix import FPIOA, GPIO\n" +
  4029. "\n" +
  4030. "_gp_side_buttons = [9, 10, 11]\n" +
  4031. "\n" +
  4032. "FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n" +
  4033. "FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n" +
  4034. "FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n" +
  4035. "\n" +
  4036. "_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n" +
  4037. "_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n" +
  4038. "_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n" +
  4039. "\n" +
  4040. "import time\n" +
  4041. "from machine import Timer\n" +
  4042. "\n" +
  4043. "_voice_button_status = False\n" +
  4044. "\n" +
  4045. "def _on_read_voice_recog_button_timer(timer):\n" +
  4046. " global _voice_button_status\n" +
  4047. " if _gp_side_" + dropdown_button_type + ".value() == True: _voice_button_status = True\n" +
  4048. " elif _gp_side_" + dropdown_button_type + ".value() == False: _voice_button_status = False\n" +
  4049. "\n" +
  4050. "_on_read_voice_recog_periodic_tim = Timer(Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PERIODIC, period=1, callback=_on_read_voice_recog_button_timer, arg=_on_read_voice_recog_button_timer)\n" +
  4051. "_on_read_voice_recog_periodic_tim.start()\n" +
  4052. "";
  4053. var code = '' +
  4054. 'while _voice_record == True:\n' +
  4055. ' print(str(_voice_button_status))\n' +
  4056. '\n' +
  4057. ' _voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords, \n' +
  4058. ' _voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
  4059. '\n' +
  4060. ' for i in range(_voice_keyword_num):\n' +
  4061. ' for j in range(_voice_model_num):\n' +
  4062. ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
  4063. ' while True:\n' +
  4064. ' if _voice_button_status == True:\n' +
  4065. ' break\n' +
  4066. ' else:\n' +
  4067. ' print(\'.\', end="")\n' +
  4068. ' sr_led_blinking("waitfor")\n' +
  4069. ' led_red.value(1)\n' +
  4070. ' s.record(i, j)\n' +
  4071. ' led_red.value(0)\n' +
  4072. ' sr_led_blinking("recorded")\n' +
  4073. ' content = str(i) + \'_\' + str(j)\n' +
  4074. ' print(content)\n' +
  4075. ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+".sr"\n' +
  4076. ' b = sr_data_save(s, content, i, j, file_name)\n' +
  4077. ' print(b)\n' +
  4078. ' print("Record successful! Next, Run your speech recognition code!")\n' +
  4079. '\n' +
  4080. ' _voice_record = False\n' +
  4081. '';
  4082. return code;
  4083. };
  4084. Blockly.Blocks['ai_audio_voice_recognition_start_recognizing'] = {
  4085. init: function() {
  4086. this.appendDummyInput()
  4087. .appendField("Start Recognizing");
  4088. this.appendValueInput("frm_num_set")
  4089. .setCheck(null)
  4090. .appendField("Set Frame Number for Each Sample:");
  4091. this.setPreviousStatement(true, null);
  4092. this.setNextStatement(true, null);
  4093. this.setColour(230);
  4094. this.setTooltip("");
  4095. this.setHelpUrl("");
  4096. }
  4097. };
  4098. Blockly.Python['ai_audio_voice_recognition_start_recognizing'] = function(block) {
  4099. var value_frm_num_set = Blockly.Python.valueToCode(block, 'frm_num_set', Blockly.Python.ORDER_ATOMIC);
  4100. // TODO: Assemble Python into code variable.
  4101. var sr_data_load_content = '';
  4102. var count = 0;
  4103. for (var i = 0; i < global_voice_recognition_word_count; i++) {
  4104. console.log(i);
  4105. for (var j = 0; j < global_voice_recognition_sample_count; j++) {
  4106. console.log(j);
  4107. sr_data_load_content += ' sr_data_load(s, ' + i + ', ' + j + ', _voice_recording_load_frm_num[' + i + '][' + j + '], "/sd/sr/' + i + '_' + j + '.sr")\n'
  4108. count += 1;
  4109. }
  4110. }
  4111. Blockly.Python.definitions_.voice_recognition_start_recognizing_def = '' +
  4112. '_voice_recording_load = True\n' +
  4113. '_voice_recording_ready = False\n' +
  4114. '\n' +
  4115. '_voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords\n' +
  4116. '_voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
  4117. '\n' +
  4118. '_voice_recording_load_frm_num = [0] * (_voice_keyword_num*_voice_model_num)\n' +
  4119. '_voice_recording_load_frm_num = ' + value_frm_num_set + '\n' +
  4120. '\n' +
  4121. 'if _voice_recording_load == True:\n' +
  4122. sr_data_load_content +
  4123. // ' sr_data_load(s, 0, 0, 86, "/sd/sr/0_0.sr")\n' +
  4124. // ' sr_data_load(s, 0, 1, 43, "/sd/sr/0_1.sr")\n' +
  4125. // ' sr_data_load(s, 0, 2, 45, "/sd/sr/0_2.sr")\n' +
  4126. // ' sr_data_load(s, 1, 0, 39, "/sd/sr/1_0.sr")\n' +
  4127. // ' sr_data_load(s, 1, 1, 82, "/sd/sr/1_1.sr")\n' +
  4128. // ' sr_data_load(s, 1, 2, 134, "/sd/sr/1_2.sr")\n' +
  4129. // ' sr_data_load(s, 2, 0, 35, "/sd/sr/2_0.sr")\n' +
  4130. // ' sr_data_load(s, 2, 1, 158, "/sd/sr/2_1.sr")\n' +
  4131. // ' sr_data_load(s, 2, 2, 87, "/sd/sr/2_2.sr")\n' +
  4132. ' print("load successful!")\n' +
  4133. '';
  4134. var code = '_voice_recognition_result = s.recognize()\n';
  4135. return code;
  4136. };
  4137. Blockly.Blocks['ai_audio_voice_recognition_get_status'] = {
  4138. init: function() {
  4139. this.appendDummyInput()
  4140. .appendField("When Get Noise is Done");
  4141. this.setOutput(true, null);
  4142. this.setColour(230);
  4143. this.setTooltip("");
  4144. this.setHelpUrl("");
  4145. }
  4146. };
  4147. Blockly.Python['ai_audio_voice_recognition_get_status'] = function(block) {
  4148. // TODO: Assemble Python into code variable.
  4149. var code = '...';
  4150. // TODO: Change ORDER_NONE to the correct strength.
  4151. return [code, Blockly.Python.ORDER_NONE];
  4152. };
  4153. Blockly.Blocks['ai_audio_voice_recognition_get_any_result'] = {
  4154. init: function() {
  4155. this.appendDummyInput()
  4156. .appendField("Get Any Result from Voice Recogntion")
  4157. this.setOutput(true, null);
  4158. this.setColour(230);
  4159. this.setTooltip("");
  4160. this.setHelpUrl("");
  4161. }
  4162. };
  4163. Blockly.Python['ai_audio_voice_recognition_get_any_result'] = function(block) {
  4164. // TODO: Assemble Python into code variable.
  4165. var code = '_voice_recognition_result > 0';
  4166. // TODO: Change ORDER_NONE to the correct strength.
  4167. return [code, Blockly.Python.ORDER_NONE];
  4168. };
  4169. Blockly.Blocks['ai_audio_voice_recognition_get_result'] = {
  4170. init: function() {
  4171. this.appendDummyInput()
  4172. .appendField("When Recognized Clip #")
  4173. .appendField(new Blockly.FieldNumber(0, 1, 6), "clip_no");
  4174. this.setOutput(true, null);
  4175. this.setColour(230);
  4176. this.setTooltip("");
  4177. this.setHelpUrl("");
  4178. }
  4179. };
  4180. Blockly.Python['ai_audio_voice_recognition_get_result'] = function(block) {
  4181. var dropdown_clip_no = block.getFieldValue('clip_no');
  4182. // TODO: Assemble Python into code variable.
  4183. var code = '_voice_recognition_result == ' + dropdown_clip_no;
  4184. // TODO: Change ORDER_NONE to the correct strength.
  4185. return [code, Blockly.Python.ORDER_NONE];
  4186. };
  4187. */
  4188. /*
  4189. _ ___ ____ _
  4190. / \ |_ _| / ___| _ __ ___ __ _| | _____ _ __
  4191. / _ \ | | \___ \| '_ \ / _ \/ _` | |/ / _ \ '__|
  4192. / ___ \ _ | | _ ___) | |_) | __/ (_| | < __/ |
  4193. /_/ \_(_)___(_) |____/| .__/ \___|\__,_|_|\_\___|_|
  4194. |_|
  4195. */
  4196. Blockly.Blocks['ai_audio_init_speaker'] = {
  4197. init: function () {
  4198. this.appendDummyInput()
  4199. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4200. this.appendDummyInput()
  4201. .appendField(Blockly.Msg.ai_audio_init);
  4202. this.setColour("#5f7fc1");
  4203. this.setTooltip(Blockly.Msg.ai_audio_init_speaker_TOOLTIP);
  4204. this.setHelpUrl("");
  4205. this.setPreviousStatement(true);
  4206. this.setNextStatement(true);
  4207. }
  4208. };
  4209. Blockly.Python.ai_audio_init_speaker = function (block) {
  4210. Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
  4211. Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
  4212. Blockly.Python.definitions_.import_audio = "import audio";
  4213. var _code = "";
  4214. return _code;
  4215. }
  4216. Blockly.Blocks['ai_audio_play_song_speaker'] = {
  4217. init: function () {
  4218. this.appendDummyInput()
  4219. .appendField(Blockly.Msg.ai_audio_play_music_file);
  4220. // this.appendDummyInput()
  4221. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  4222. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  4223. this.appendValueInput("volume")
  4224. .setCheck(null)
  4225. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  4226. this.appendValueInput("path")
  4227. .setCheck(null)
  4228. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  4229. this.setPreviousStatement(true, null);
  4230. this.setNextStatement(true, null);
  4231. this.setColour("#5f7fc1");
  4232. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_TOOLTIP);
  4233. this.setHelpUrl("");
  4234. }
  4235. };
  4236. Blockly.Python['ai_audio_play_song_speaker'] = function (block) {
  4237. // var number_play_vol = block.getFieldValue('play_vol');
  4238. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  4239. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4240. var _in = "" +
  4241. "_audio_rx = I2S(I2S.DEVICE_0)\n" +
  4242. "\n" +
  4243. "def _speaker_play_song(path, volume):\n" +
  4244. " global _audio_play_state,_player\n" +
  4245. " _player = audio.Audio(path = path)\n" +
  4246. " _player.volume(volume)\n" +
  4247. " try:\n" +
  4248. " _wav_info = _player.play_process(_audio_rx)\n" +
  4249. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  4250. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  4251. " _audio_play_state = 0\n" +
  4252. "\n" +
  4253. " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
  4254. " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
  4255. " fm.register(33,fm.fpioa.I2S0_WS)\n" +
  4256. "\n" +
  4257. " while True:\n" +
  4258. " _audio_play_state = _player.play()\n" +
  4259. " if (_audio_play_state) == True:\n" +
  4260. " print(\"Playing.\")\n" +
  4261. " elif (_audio_play_state) == False:\n" +
  4262. " print(\"Done.\")\n" +
  4263. " break\n" +
  4264. "\n" +
  4265. " except:\n" +
  4266. " pass\n" +
  4267. "\n";
  4268. // Blockly.Python.definitions_.import_audio = _in;
  4269. Blockly.Python.addVariable('_audio_play_state', _in, true);
  4270. // Blockly.Python.addSetup("ai_audio_read", _in);
  4271. var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
  4272. return _code;
  4273. };
  4274. Blockly.Blocks['ai_audio_play_state_speaker'] = {
  4275. init: function () {
  4276. this.appendDummyInput()
  4277. .appendField(Blockly.Msg.ai_audio_get_play_status);
  4278. this.setOutput(true, null);
  4279. this.setColour("#5f7fc1");
  4280. this.setTooltip(Blockly.Msg.ai_audio_play_state_speaker_TOOLTIP);
  4281. this.setHelpUrl("");
  4282. }
  4283. };
  4284. Blockly.Python['ai_audio_play_state_speaker'] = function (block) {
  4285. // TODO: Assemble Python into code variable.
  4286. var code = '_audio_play_state';
  4287. // TODO: Change ORDER_NONE to the correct strength.
  4288. return [code, Blockly.Python.ORDER_NONE];
  4289. };
  4290. Blockly.Blocks['ai_audio_play_finish_speaker'] = {
  4291. init: function () {
  4292. this.appendDummyInput()
  4293. .appendField(Blockly.Msg.ai_audio_end_play);
  4294. this.setPreviousStatement(true, null);
  4295. this.setNextStatement(true, null);
  4296. this.setColour("#5f7fc1");
  4297. this.setTooltip(Blockly.Msg.ai_audio_play_finish_speaker_TOOLTIP);
  4298. this.setHelpUrl("");
  4299. }
  4300. };
  4301. Blockly.Python['ai_audio_play_finish_speaker'] = function (block) {
  4302. // TODO: Assemble Python into code variable.
  4303. var code = '_player.finish()\n';
  4304. return code;
  4305. };
  4306. Blockly.Blocks['ai_audio_set_speaker'] = {
  4307. init: function () {
  4308. this.appendValueInput("Num")
  4309. .setCheck(null)
  4310. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  4311. this.setInputsInline(true);
  4312. this.setPreviousStatement(true, null);
  4313. this.setNextStatement(true, null);
  4314. this.setColour("#5f7fc1");
  4315. this.setTooltip(Blockly.Msg.ai_audio_set_speaker_TOOLTIP);
  4316. this.setHelpUrl("");
  4317. }
  4318. };
  4319. Blockly.Python.ai_audio_set_speaker = function (block) {
  4320. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  4321. var _code = "_player.volume(" + _num + ")\n"
  4322. return _code;
  4323. }
  4324. Blockly.Blocks['ai_audio_files_list'] = {
  4325. init: function () {
  4326. this.appendDummyInput()
  4327. .appendField(Blockly.Msg.ai_audio_files_list);
  4328. this.appendValueInput("path")
  4329. .setCheck(null)
  4330. .appendField(Blockly.Msg.ai_audio_files_list_set_file_path);
  4331. this.setOutput(true, null);
  4332. this.setColour("#5f7fc1");
  4333. this.setTooltip(Blockly.Msg.ai_audio_files_list_TOOLTIP);
  4334. this.setHelpUrl("");
  4335. }
  4336. };
  4337. Blockly.Python.ai_audio_files_list = function (block) {
  4338. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4339. Blockly.Python.definitions_.import_ai_audio_files_list = '' +
  4340. 'import os\n'+
  4341. '\n' +
  4342. 'def getRawFileList(path):\n' +
  4343. ' files = []\n' +
  4344. ' for f in os.listdir(path):\n' +
  4345. ' if (not f.endswith("~") or not f == "") and f.split(".")[1] == "wav":\n' +
  4346. ' files.append(path + "/" + f)\n' +
  4347. ' return files\n' +
  4348. '\n';
  4349. var code = 'getRawFileList(' + value_name +')';
  4350. return [code, Blockly.Python.ORDER_NONE];
  4351. };
  4352. Blockly.Blocks['ai_audio_play_song_list_loop_speaker'] = {
  4353. init: function () {
  4354. this.appendDummyInput()
  4355. .appendField(Blockly.Msg.ai_audio_play_music_file_list_loop);
  4356. // this.appendDummyInput()
  4357. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  4358. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  4359. this.appendValueInput("volume")
  4360. .setCheck(null)
  4361. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  4362. this.appendValueInput("path")
  4363. .setCheck(null)
  4364. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  4365. this.appendValueInput("my_list")
  4366. .setCheck(null)
  4367. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  4368. this.appendValueInput("firstIndex")
  4369. .setCheck(null)
  4370. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  4371. this.setPreviousStatement(true, null);
  4372. this.setNextStatement(true, null);
  4373. this.setColour("#5f7fc1");
  4374. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_list_loop_TOOLTIP);
  4375. this.setHelpUrl("");
  4376. }
  4377. };
  4378. Blockly.Python['ai_audio_play_song_list_loop_speaker'] = function (block) {
  4379. // var number_play_vol = block.getFieldValue('play_vol');
  4380. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  4381. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4382. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  4383. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  4384. var _in = "" +
  4385. "_audio_rx = I2S(I2S.DEVICE_0)\n" +
  4386. "\n" +
  4387. "# 上一首\n" +
  4388. "def previous_song(index,my_list):\n"+
  4389. " global direction\n" +
  4390. " index = index - 1\n"+
  4391. " direction = 1\n" +
  4392. " if index == -1:\n"+
  4393. " index = len(my_list) - 1\n"+
  4394. " return index\n" +
  4395. "\n"+
  4396. "# 切换上一首\n" +
  4397. "def change_previous_song(index):\n"+
  4398. " global _audio_play_state,direction,firstIndex,_player\n" +
  4399. " _player.finish()\n" +
  4400. " _audio_play_state = 0\n" +
  4401. " firstIndex = index\n" +
  4402. " direction = 1\n" +
  4403. "\n"+
  4404. "# 下一首\n" +
  4405. "def next_song(index,my_list):\n"+
  4406. " global direction\n" +
  4407. " index = index + 1\n"+
  4408. " direction = 2\n" +
  4409. " if index == len(my_list):\n" +
  4410. " index = 0\n" +
  4411. " return index\n" +
  4412. "\n"+
  4413. "# 切换下一首\n" +
  4414. "def change_next_song(index):\n"+
  4415. " global _audio_play_state,direction,firstIndex,_player\n" +
  4416. " _player.finish()\n" +
  4417. " _audio_play_state = 0\n" +
  4418. " firstIndex = index\n" +
  4419. " direction = 2\n" +
  4420. "\n"+
  4421. "def _speaker_play_song_list_loop(path, volume, my_list, index):\n" +
  4422. " global _audio_play_state,_player,firstIndex,play_again,direction\n" +
  4423. " _player = audio.Audio(path = path)\n" +
  4424. " _player.volume(volume)\n" +
  4425. " try:\n" +
  4426. " firstIndex = index\n" +
  4427. " _wav_info = _player.play_process(_audio_rx)\n" +
  4428. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  4429. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  4430. " _audio_play_state = 0\n" +
  4431. "\n" +
  4432. " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
  4433. " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
  4434. " fm.register(33,fm.fpioa.I2S0_WS)\n" +
  4435. "\n" +
  4436. " button_a_state = False\n" +
  4437. " button_b_state = False\n" +
  4438. " while True:\n" +
  4439. " _audio_play_state = _player.play()\n" +
  4440. " if (_audio_play_state) == True:\n" +
  4441. " #print(\"Playing.\")\n" +
  4442. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  4443. " index = previous_song(index,my_list)\n" +
  4444. " button_a_state = True\n" +
  4445. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  4446. " button_a_state = False\n" +
  4447. " change_previous_song(index)\n" +
  4448. " break\n" +
  4449. " if (_gp_side_b.value() == 1) and button_b_state == False:\n" +
  4450. " index = next_song(index,my_list)\n"+
  4451. " button_b_state = True\n" +
  4452. " elif (_gp_side_b.value() == 0) and button_b_state == True:\n" +
  4453. " button_b_state = False\n" +
  4454. " change_next_song(index)\n" +
  4455. " break\n" +
  4456. "\n" +
  4457. " elif (_audio_play_state) == False:\n" +
  4458. " #print(\"Done.\")\n" +
  4459. " index = index + 1\n" +
  4460. " if index == len(my_list):\n" +
  4461. " index = 0\n" +
  4462. " firstIndex = index\n" +
  4463. " break\n" +
  4464. "\n" +
  4465. " except:\n" +
  4466. " play_again = True\n" +
  4467. " if direction == 0:\n" +
  4468. " direction = 3\n" +
  4469. " return\n" +
  4470. "\n";
  4471. // Blockly.Python.definitions_.import_audio = _in;
  4472. Blockly.Python.addVariable('_audio_play_list_loop_state', _in, true);
  4473. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  4474. 'from fpioa_manager import *\n' +
  4475. 'from Maix import FPIOA, GPIO\n' +
  4476. '\n' +
  4477. '_gp_side_buttons = [9, 10, 11]\n' +
  4478. '\n' +
  4479. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  4480. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  4481. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  4482. '\n' +
  4483. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  4484. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  4485. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  4486. '\n';
  4487. // Blockly.Python.addSetup("ai_audio_read", _in);
  4488. var _code = "_speaker_play_song_list_loop(path=" + value_name + ", volume=" + number_play_vol + ", my_list=" + my_list + ", index=" + firstIndex + ")\n";
  4489. return _code;
  4490. };
  4491. Blockly.Blocks['ai_audio_play_song_list_random_speaker'] = {
  4492. init: function () {
  4493. this.appendDummyInput()
  4494. .appendField(Blockly.Msg.ai_audio_play_music_file_list_random);
  4495. // this.appendDummyInput()
  4496. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  4497. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  4498. this.appendValueInput("volume")
  4499. .setCheck(null)
  4500. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  4501. this.appendValueInput("path")
  4502. .setCheck(null)
  4503. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  4504. this.appendValueInput("my_list")
  4505. .setCheck(null)
  4506. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  4507. this.appendValueInput("firstIndex")
  4508. .setCheck(null)
  4509. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  4510. this.setPreviousStatement(true, null);
  4511. this.setNextStatement(true, null);
  4512. this.setColour("#5f7fc1");
  4513. this.setTooltip(Blockly.Msg.ai_audio_play_music_file_list_random_TOOLTIP);
  4514. this.setHelpUrl("");
  4515. }
  4516. };
  4517. Blockly.Python['ai_audio_play_song_list_random_speaker'] = function (block) {
  4518. // var number_play_vol = block.getFieldValue('play_vol');
  4519. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  4520. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4521. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  4522. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  4523. var _in = "" +
  4524. "import time\n" +
  4525. "import random\n" +
  4526. "\n" +
  4527. "random.seed(time.ticks_cpu())\n" +
  4528. "\n" +
  4529. "_audio_rx = I2S(I2S.DEVICE_0)\n" +
  4530. "\n" +
  4531. "# 随机一首\n" +
  4532. "def random_song(index,my_list):\n"+
  4533. " index = random.randrange(0, len(my_list), 1)\n"+
  4534. " return index\n" +
  4535. "\n"+
  4536. "# 切换上一首\n" +
  4537. "def change_random_song(index):\n"+
  4538. " global _audio_play_state,firstIndex,_player\n" +
  4539. " _player.finish()\n" +
  4540. " _audio_play_state = 0\n" +
  4541. " firstIndex = index\n" +
  4542. "\n"+
  4543. "def _speaker_play_song_list_random(path, volume, my_list, index):\n" +
  4544. " global _audio_play_state,_player,firstIndex,play_again\n" +
  4545. " _player = audio.Audio(path = path)\n" +
  4546. " _player.volume(volume)\n" +
  4547. " try:\n" +
  4548. " firstIndex = index\n" +
  4549. " _wav_info = _player.play_process(_audio_rx)\n" +
  4550. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  4551. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  4552. " _audio_play_state = 0\n" +
  4553. "\n" +
  4554. " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
  4555. " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
  4556. " fm.register(33,fm.fpioa.I2S0_WS)\n" +
  4557. "\n" +
  4558. " button_a_state = False\n" +
  4559. " while True:\n" +
  4560. " _audio_play_state = _player.play()\n" +
  4561. " if (_audio_play_state) == True:\n" +
  4562. " #print(\"Playing.\")\n" +
  4563. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  4564. " index = random_song(index,my_list)\n" +
  4565. " button_a_state = True\n" +
  4566. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  4567. " button_a_state = False\n" +
  4568. " change_random_song(index)\n" +
  4569. " break\n" +
  4570. "\n" +
  4571. " elif (_audio_play_state) == False:\n" +
  4572. " #print(\"Done.\")\n" +
  4573. " index = random_song(index,my_list)\n" +
  4574. " firstIndex = index\n" +
  4575. " break\n" +
  4576. "\n" +
  4577. " except:\n" +
  4578. " play_again = True\n" +
  4579. " return\n" +
  4580. "\n";
  4581. // Blockly.Python.definitions_.import_audio = _in;
  4582. Blockly.Python.addVariable('_audio_play_list_random_state', _in, true);
  4583. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  4584. 'from fpioa_manager import *\n' +
  4585. 'from Maix import FPIOA, GPIO\n' +
  4586. '\n' +
  4587. '_gp_side_buttons = [9, 10, 11]\n' +
  4588. '\n' +
  4589. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  4590. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  4591. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  4592. '\n' +
  4593. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  4594. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  4595. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  4596. '\n';
  4597. // Blockly.Python.addSetup("ai_audio_read", _in);
  4598. var _code = "_speaker_play_song_list_random(path=" + value_name + ", volume=" + number_play_vol + ", my_list=" + my_list + ", index=" + firstIndex + ")\n";
  4599. return _code;
  4600. };
  4601. /*
  4602. _ ___ __ ___ _
  4603. / \ |_ _| \ \ / (_) __| | ___ ___
  4604. / _ \ | | \ \ / /| |/ _` |/ _ \/ _ \
  4605. / ___ \ | | \ V / | | (_| | __/ (_) |
  4606. /_/ \_\___| \_/ |_|\__,_|\___|\___/
  4607. */
  4608. Blockly.Blocks['ai_video_record_setup'] = {
  4609. init: function () {
  4610. this.appendDummyInput()
  4611. .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4612. this.appendDummyInput()
  4613. .appendField(Blockly.Msg.ai_video_start_recording);
  4614. this.appendValueInput("save_path")
  4615. .setCheck(null)
  4616. .appendField(Blockly.Msg.ai_video_start_recording_file_path);
  4617. this.appendValueInput("w")
  4618. .setCheck(null)
  4619. .appendField(Blockly.Msg.ai_video_start_recording_width);
  4620. this.appendValueInput("h")
  4621. .setCheck(null)
  4622. .appendField(Blockly.Msg.ai_video_start_recording_height);
  4623. this.setPreviousStatement(true, null);
  4624. this.setNextStatement(true, null);
  4625. this.setColour("#60c1bb");
  4626. this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
  4627. this.setHelpUrl("");
  4628. }
  4629. };
  4630. Blockly.Python['ai_video_record_setup'] = function (block) {
  4631. var value_save_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  4632. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  4633. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  4634. Blockly.Python.definitions_.import_video = "import video";
  4635. Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
  4636. Blockly.Python.addVariable('record_vid', 'record_vid = None', true);
  4637. // TODO: Assemble Python into code variable.
  4638. var code = "_record_vid = video.open(" + value_save_path + ", record=1, width=" + value_w + ", height=" + value_h + ", interval=20000, quality=80)\n";
  4639. return code;
  4640. };
  4641. Blockly.Blocks['ai_video_get'] = {
  4642. init: function () {
  4643. this.appendDummyInput()
  4644. .appendField(Blockly.Msg.ai_video_to_canvas)
  4645. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  4646. .appendField(Blockly.Msg.ai_video_record);
  4647. this.setInputsInline(false);
  4648. this.setPreviousStatement(true, null);
  4649. this.setNextStatement(true, null);
  4650. this.setColour("#60c1bb");
  4651. this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
  4652. this.setHelpUrl("");
  4653. }
  4654. };
  4655. Blockly.Python.ai_video_get = function (block) {
  4656. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4657. var _code = "_record_vid.record(" + variable_name + ")\n"
  4658. return _code;
  4659. }
  4660. Blockly.Blocks['ai_video_stop'] = {
  4661. init: function () {
  4662. this.appendDummyInput()
  4663. .appendField(Blockly.Msg.ai_video_stop_recording);
  4664. this.setPreviousStatement(true);
  4665. this.setNextStatement(true);
  4666. this.setColour("#60c1bb");
  4667. this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
  4668. this.setHelpUrl("");
  4669. }
  4670. };
  4671. Blockly.Python.ai_video_stop = function (block) {
  4672. var _code = "_record_vid.record_finish()\n"
  4673. return _code;
  4674. }
  4675. Blockly.Blocks['ai_video_set'] = {
  4676. init: function () {
  4677. this.appendDummyInput()
  4678. .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4679. this.appendDummyInput()
  4680. .appendField(Blockly.Msg.ai_video_play_video);
  4681. this.appendValueInput("path")
  4682. .setCheck(null)
  4683. .appendField(Blockly.Msg.ai_video_play_video_file_path);
  4684. this.appendDummyInput()
  4685. .appendField(Blockly.Msg.ai_video_play_video_2);
  4686. this.setPreviousStatement(true, null);
  4687. this.setNextStatement(true, null);
  4688. this.setColour("#4d9994");
  4689. this.setTooltip(Blockly.Msg.ai_video_set_TOOLTIP);
  4690. this.setHelpUrl("");
  4691. }
  4692. };
  4693. Blockly.Python['ai_video_set'] = function (block) {
  4694. Blockly.Python.definitions_.import_video = "import video";
  4695. Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
  4696. Blockly.Python.addVariable('_video_play', '', true);
  4697. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4698. var code = '_video_play = video.open(' + value_path + ')\n';
  4699. return code;
  4700. };
  4701. Blockly.Blocks['ai_video_stop_play'] = {
  4702. init: function () {
  4703. this.appendDummyInput()
  4704. .appendField(Blockly.Msg.ai_video_stop_video_playing);
  4705. this.setPreviousStatement(true, null);
  4706. this.setNextStatement(true, null);
  4707. this.setColour("#4d9994");
  4708. this.setTooltip(Blockly.Msg.ai_video_stop_play_TOOLTIP);
  4709. this.setHelpUrl("");
  4710. }
  4711. };
  4712. Blockly.Python['ai_video_stop_play'] = function (block) {
  4713. // TODO: Assemble Python into code variable.
  4714. var code = '_video_play.__del__()\n';
  4715. return code;
  4716. };
  4717. Blockly.Blocks['ai_video_status'] = {
  4718. init: function () {
  4719. this.appendDummyInput()
  4720. .appendField(Blockly.Msg.ai_video_get_video_playing_status);
  4721. this.setOutput(true, null);
  4722. this.setColour("#4d9994");
  4723. this.setTooltip(Blockly.Msg.ai_video_status_TOOLTIP);
  4724. this.setHelpUrl("");
  4725. }
  4726. };
  4727. Blockly.Python.ai_video_status = function (block) {
  4728. var code = "_video_play.play()";
  4729. return [code, Blockly.Python.ORDER_NONE];
  4730. }
  4731. /*
  4732. _ ___ __ __ _ _
  4733. / \ |_ _| | \/ | ___ __| | ___| |___
  4734. / _ \ | | | |\/| |/ _ \ / _` |/ _ \ / __|
  4735. / ___ \ | | | | | | (_) | (_| | __/ \__ \
  4736. /_/ \_\___| |_| |_|\___/ \__,_|\___|_|___/
  4737. */
  4738. Blockly.Blocks['ai_model_allinit'] = {
  4739. init: function () {
  4740. this.appendDummyInput()
  4741. .appendField(Blockly.Msg.ai_models_init_title)
  4742. .appendField(new Blockly.FieldDropdown([
  4743. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
  4744. [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
  4745. [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
  4746. ]), "DEGREE");
  4747. this.appendDummyInput()
  4748. .appendField(Blockly.Msg.ai_models_init_source)
  4749. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  4750. this.setPreviousStatement(true, null);
  4751. this.setNextStatement(true, null);
  4752. this.setColour("#ee783a");
  4753. var thisBlock = this;
  4754. this.setTooltip(function () {
  4755. var mode = thisBlock.getFieldValue('DEGREE');
  4756. var TOOLTIPS = {
  4757. 'Single Digit Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
  4758. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
  4759. 'Human Face Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  4760. };
  4761. return TOOLTIPS[mode];
  4762. });
  4763. this.setHelpUrl('');
  4764. }
  4765. };
  4766. Blockly.Python.ai_model_allinit = function (block) {
  4767. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4768. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  4769. var degee = block.getFieldValue('DEGREE');
  4770. var _code = ""
  4771. if (degee == "Single Digit Recognition") {
  4772. Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
  4773. // Blockly.Python.addVariable('img_mnist1', '', true);
  4774. // Blockly.Python.addVariable('img_mnist_resized', '', true);
  4775. _code += "img_mnist1=img_mnist.to_grayscale(1)\n";
  4776. _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
  4777. _code += "a=img_mnist_resized.invert()\n";
  4778. _code += "a=img_mnist_resized.strech_char(1)\n";
  4779. _code += "a=img_mnist_resized.pix_to_ai()\n";
  4780. _code += "fmap_mnist=kpu.forward(task_mnist, " + variable_name + ")\n";
  4781. _code += "plist_mnist=fmap_mnist[:]\n";
  4782. _code += "pmax_mnist=max(plist_mnist)\n";
  4783. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
  4784. } else if (degee == "Common Object Recognition") {
  4785. Blockly.Python.addVariable('task_objectrecognition', '', true);
  4786. Blockly.Python.addVariable('img_objectrecognition', '' +
  4787. 'classes_objectrecognition = [\'aeroplane\', \'bicycle\', \'bird\', \'boat\', \'bottle\', \'bus\', \'car\', \'cat\', \'chair\', \'cow\', \'diningtable\', \'dog\', \'horse\', \'motorbike\', \'person\', \'pottedplant\', \'sheep\', \'sofa\', \'train\', \'tvmonitor\']\n' +
  4788. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  4789. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  4790. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  4791. '', true);
  4792. // Blockly.Python.addVariable('code_objectrecognition', '', true);
  4793. _code += "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n";
  4794. _code += variable_name + ".ai_to_pix()\n";
  4795. } else {
  4796. Blockly.Python.addVariable('task_facerecognition', '', true);
  4797. // Blockly.Python.addVariable('task_facerecognition', '', true);
  4798. Blockly.Python.addVariable('img_facerecognition', "" +
  4799. "task_facerecognition = kpu.load(\"/sd/preset/models/preset/face-recognition.kmodel\")\n" +
  4800. "anchor_face = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n" +
  4801. "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor_face)\n" +
  4802. "\n", true);
  4803. // Blockly.Python.addVariable('code_facerecognition', '', true);
  4804. _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
  4805. _code += variable_name + ".ai_to_pix()\n";
  4806. }
  4807. return _code;
  4808. }
  4809. Blockly.Blocks['ai_model_index_mnist'] = {
  4810. init: function () {
  4811. this.appendDummyInput()
  4812. .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4813. this.appendDummyInput()
  4814. .appendField(Blockly.Msg.ai_models_hand_written_model_result);
  4815. this.appendDummyInput()
  4816. .appendField(new Blockly.FieldDropdown([
  4817. [Blockly.Msg.ai_models_hand_written_model_number, "c"],
  4818. [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"]
  4819. ]), "value");
  4820. this.setOutput(true, null);
  4821. this.setColour("#ee783a");
  4822. this.setTooltip(Blockly.Msg.ai_model_index_mnist_TOOLTIP);
  4823. this.setHelpUrl('');
  4824. }
  4825. };
  4826. Blockly.Python.ai_model_index_mnist = function (block) {
  4827. var value = block.getFieldValue('value');
  4828. var _code = "max_index_mnist";
  4829. if (value == "confidence") {
  4830. _code = "int(pmax_mnist*100)";
  4831. }
  4832. return [_code, Blockly.Python.ORDER_ATOMIC];
  4833. }
  4834. Blockly.Blocks['ai_model_look'] = {
  4835. init: function () {
  4836. this.appendDummyInput()
  4837. .appendField(Blockly.Msg.ai_models_object_model_if);
  4838. this.setInputsInline(false);
  4839. this.setOutput(true, null);
  4840. this.setColour("#ee783a");
  4841. this.setTooltip(Blockly.Msg.ai_model_look_TOOLTIP);
  4842. this.setHelpUrl("");
  4843. }
  4844. };
  4845. Blockly.Python.ai_model_look = function (block) {
  4846. var _code = "code_objectrecognition";
  4847. return [_code, Blockly.Python.ORDER_ATOMIC];
  4848. }
  4849. Blockly.Blocks['ai_model_get'] = {
  4850. init: function () {
  4851. this.appendDummyInput()
  4852. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4853. this.appendDummyInput()
  4854. .appendField(Blockly.Msg.ai_models_from)
  4855. .appendField(new Blockly.FieldVariable("i"), "varitem")
  4856. .appendField(Blockly.Msg.ai_models_object_model_get)
  4857. .appendField(new Blockly.FieldDropdown([
  4858. [Blockly.Msg.ai_models_object_model_name, "Object"],
  4859. [Blockly.Msg.ai_models_object_model_x, "X"],
  4860. [Blockly.Msg.ai_models_object_model_y, "Y"],
  4861. [Blockly.Msg.ai_models_object_model_w, "W"],
  4862. [Blockly.Msg.ai_models_object_model_h, "H"],
  4863. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  4864. [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  4865. [Blockly.Msg.ai_models_object_model_count, "Number"],
  4866. [Blockly.Msg.ai_models_object_model_index, "Serial"],
  4867. [Blockly.Msg.ai_models_object_model_confidence, "Confidence"]
  4868. ]), "TYPE");
  4869. this.setInputsInline(false);
  4870. this.setOutput(true, null);
  4871. this.setColour("#ee783a");
  4872. var thisBlock = this;
  4873. this.setTooltip(function () {
  4874. var mode = thisBlock.getFieldValue('TYPE');
  4875. var TOOLTIPS = {
  4876. 'Object': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_name),
  4877. 'X': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x),
  4878. 'Y': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y),
  4879. 'W': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_w),
  4880. 'H': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_h),
  4881. 'CX': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x_center),
  4882. 'CY': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y_center),
  4883. 'Number': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_count),
  4884. 'Serial': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_index),
  4885. 'Confidence': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_confidence)
  4886. };
  4887. return TOOLTIPS[mode];
  4888. });
  4889. this.setHelpUrl("");
  4890. }
  4891. };
  4892. Blockly.Python.ai_model_get = function (block) {
  4893. var type = block.getFieldValue('TYPE');
  4894. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4895. var _code = "";
  4896. if (type == "Object") {
  4897. _code = "classes_objectrecognition[" + variable_name + ".classid()]";
  4898. } else if (type == "X") {
  4899. _code = variable_name + ".x()";
  4900. } else if (type == "Y") {
  4901. _code = variable_name + ".y()";
  4902. } else if (type == "W") {
  4903. _code = variable_name + ".w()";
  4904. } else if (type == "H") {
  4905. _code = variable_name + ".h()";
  4906. } else if (type == "Number") {
  4907. _code = variable_name + ".objnum()";
  4908. } else if (type == "Serial") {
  4909. _code = variable_name + ".index()";
  4910. } else if (type == "Confidence") {
  4911. _code = variable_name + ".value()";
  4912. } else if (type == "CX") {
  4913. _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  4914. } else if (type == "CY") {
  4915. _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  4916. }
  4917. return [_code, Blockly.Python.ORDER_ATOMIC];
  4918. }
  4919. Blockly.Blocks['ai_model_face'] = {
  4920. init: function () {
  4921. this.appendDummyInput()
  4922. .appendField(Blockly.Msg.ai_models_face_model_if);
  4923. this.setInputsInline(false);
  4924. this.setOutput(true, null);
  4925. this.setColour("#ee783a");
  4926. this.setTooltip(Blockly.Msg.ai_model_face_TOOLTIP);
  4927. this.setHelpUrl("");
  4928. }
  4929. };
  4930. Blockly.Python.ai_model_face = function (block) {
  4931. var _code = "code_facerecognition";
  4932. return [_code, Blockly.Python.ORDER_ATOMIC];
  4933. }
  4934. Blockly.Blocks['ai_model_xy'] = {
  4935. init: function () {
  4936. this.appendDummyInput()
  4937. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4938. this.appendDummyInput()
  4939. .appendField(Blockly.Msg.ai_models_from)
  4940. .appendField(new Blockly.FieldVariable("i"), "varitem")
  4941. .appendField(Blockly.Msg.ai_models_face_model_get)
  4942. .appendField(new Blockly.FieldDropdown([
  4943. [Blockly.Msg.ai_models_face_model_x, "X"],
  4944. [Blockly.Msg.ai_models_face_model_y, "Y"],
  4945. [Blockly.Msg.ai_models_face_model_w, "W"],
  4946. [Blockly.Msg.ai_models_face_model_h, "H"],
  4947. [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  4948. [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  4949. [Blockly.Msg.ai_models_face_model_count, "Number"],
  4950. [Blockly.Msg.ai_models_face_model_index, "Serial"],
  4951. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
  4952. ]), "TYPE");
  4953. this.setInputsInline(false);
  4954. this.setOutput(true, null);
  4955. this.setColour("#ee783a");
  4956. var thisBlock = this;
  4957. this.setTooltip(function () {
  4958. var mode = thisBlock.getFieldValue('TYPE');
  4959. var TOOLTIPS = {
  4960. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  4961. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  4962. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  4963. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  4964. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  4965. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  4966. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  4967. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  4968. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  4969. };
  4970. return TOOLTIPS[mode];
  4971. });
  4972. this.setHelpUrl("");
  4973. }
  4974. };
  4975. Blockly.Python.ai_model_xy = function (block) {
  4976. var type = block.getFieldValue('TYPE');
  4977. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4978. var _code = "";
  4979. if (type == "X") {
  4980. _code = variable_name + ".x()";
  4981. } else if (type == "Y") {
  4982. _code = variable_name + ".y()";
  4983. } else if (type == "W") {
  4984. _code = variable_name + ".w()";
  4985. } else if (type == "H") {
  4986. _code = variable_name + ".h()";
  4987. } else if (type == "Number") {
  4988. _code = variable_name + ".objnum()";
  4989. } else if (type == "Serial") {
  4990. _code = variable_name + ".index()";
  4991. } else if (type == "Confidence") {
  4992. _code = variable_name + ".value()";
  4993. } else if (type == "CX") {
  4994. _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  4995. } else if (type == "CY") {
  4996. _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  4997. }
  4998. return [_code, Blockly.Python.ORDER_ATOMIC];
  4999. };
  5000. Blockly.Blocks['ai_model_customized_init_setup_definition'] = {
  5001. init: function () {
  5002. this.appendDummyInput()
  5003. .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  5004. this.appendDummyInput()
  5005. .appendField(Blockly.Msg.ai_model_customized_init_setup_definition);
  5006. this.setInputsInline(false);
  5007. this.setPreviousStatement(true, null);
  5008. this.setNextStatement(true, null);
  5009. this.setColour("#ee783a");
  5010. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  5011. this.setHelpUrl("");
  5012. }
  5013. };
  5014. Blockly.Python['ai_model_customized_init_setup_definition'] = function (block) {
  5015. // TODO: Assemble Python into code variable.
  5016. Blockly.Python.includes_.import_amxisd = '' +
  5017. 'print("Clearing Cached Variables...", end="")\n' +
  5018. 'for name in dir(): \n' +
  5019. ' if not name.startswith(\'_\'): \n' +
  5020. ' del globals()[name]\n' +
  5021. 'print(" Done")\n' +
  5022. 'import KPU as kpu\n' +
  5023. 'kpu.memtest()\n' +
  5024. 'from Maix import utils\n' +
  5025. 'import gc\n' +
  5026. 'gc.enable()\n' +
  5027. 'utils.gc_heap_size()\n' +
  5028. '\n' +
  5029. '################# Done Init #################\n' +
  5030. '';
  5031. var code = '';
  5032. return code;
  5033. };
  5034. Blockly.Blocks['ai_model_customized_init_setup'] = {
  5035. init: function () {
  5036. this.appendDummyInput()
  5037. .appendField(Blockly.Msg.ai_models_customized_init_setup_title);
  5038. this.appendDummyInput()
  5039. .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
  5040. .appendField(new Blockly.FieldTextInput("/sd/user/mymodel.kmodel"), "model_path");
  5041. this.appendDummyInput()
  5042. .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  5043. .appendField(new Blockly.FieldVariable("camera"), "varitem");
  5044. this.appendValueInput("class_input")
  5045. .setCheck(null)
  5046. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  5047. this.setPreviousStatement(true, null);
  5048. this.setNextStatement(true, null);
  5049. this.setColour("#ee783a");
  5050. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_TOOLTIP);
  5051. this.setHelpUrl("");
  5052. }
  5053. };
  5054. Blockly.Python['ai_model_customized_init_setup'] = function (block) {
  5055. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  5056. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5057. var text_model_path = block.getFieldValue('model_path');
  5058. Blockly.Python.definitions_.import_KPU_customized = "import KPU as kpu";
  5059. Blockly.Python.addVariable('task_customized_model', '' +
  5060. 'task_customized_model = kpu.load("' + text_model_path + '")\n' +
  5061. 'anchor_customized_model = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828)\n' +
  5062. 'a = kpu.init_yolo2(task_customized_model, 0.6, 0.3, 5, anchor_customized_model)\n' +
  5063. '', true);
  5064. Blockly.Python.addVariable('classes_customized_model', '' +
  5065. 'classes_customized_model = ' + value_class_input + '\n' +
  5066. '', true);
  5067. var code = 'code_customized_model = kpu.run_yolo2(task_customized_model, ' + variable_name + ')\n';
  5068. return code;
  5069. };
  5070. Blockly.Blocks['ai_model_face_init'] = {
  5071. init: function () {
  5072. this.appendDummyInput()
  5073. .appendField(Blockly.Msg.ai_model_face);
  5074. this.appendDummyInput()
  5075. .appendField(Blockly.Msg.ai_models_customized_init_type_set)
  5076. .appendField(new Blockly.FieldDropdown([
  5077. [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
  5078. [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
  5079. ]), "detect_type");
  5080. this.appendDummyInput()
  5081. .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  5082. .appendField(new Blockly.FieldVariable("img"), "varitem");
  5083. this.appendValueInput("class_input")
  5084. .setCheck(null)
  5085. .appendField(Blockly.Msg.ai_model_face_name);
  5086. this.setPreviousStatement(true, null);
  5087. this.setNextStatement(true, null);
  5088. this.setColour("#ee783a");
  5089. this.setTooltip(Blockly.Msg.ai_model_face_init_TOOLTIP);
  5090. this.setHelpUrl("");
  5091. }
  5092. };
  5093. Blockly.Python['ai_model_face_init'] = function(block) {
  5094. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5095. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  5096. var dropdown_detect_type = block.getFieldValue('detect_type');
  5097. if ( dropdown_detect_type == "fast" ) {
  5098. var break_string = "break\n";
  5099. } else if ( dropdown_detect_type == "slow" ) {
  5100. var break_string = "# break\n";
  5101. }
  5102. Blockly.Python.includes_.import_modelface = `
  5103. for name in dir():
  5104. if not name.startswith('_'):
  5105. del globals()[name]
  5106. import gc
  5107. import KPU as kpu
  5108. from Maix import utils
  5109. utils.gc_heap_size(0xAF000)
  5110. import sensor
  5111. import image
  5112. import lcd
  5113. import time
  5114. from Maix import FPIOA, GPIO
  5115. import gc
  5116. from fpioa_manager import fm
  5117. from board import board_info
  5118. try:from cocorobo import display_cjk_string
  5119. except:pass
  5120. def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
  5121. try:
  5122. display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
  5123. return canvas
  5124. except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
  5125. Blockly.Python.addFunction("set_key_state", `def set_key_state(*_):
  5126. global start_processing
  5127. start_processing = True`);
  5128. Blockly.Python.addVariable('task_f', `
  5129. gc.enable()
  5130. gc.collect()
  5131. # kpu.memtest()
  5132. task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel")
  5133. task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel")
  5134. task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel")
  5135. #kpu.set_outputs(task, 0, 1,1,1) # 配置输出形状
  5136. kpu.set_outputs(task_fe, 0, 1,1,512)
  5137. # kpu.memtest()
  5138. kpu.set_outputs(task_fe, 0,1,1, 512)
  5139. fm.register(10, fm.fpioa.GPIOHS0)
  5140. key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
  5141. start_processing = False
  5142. key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
  5143. lcd.init(type=2)
  5144. lcd.rotation(1)
  5145. sensor.reset()
  5146. sensor.set_pixformat(sensor.RGB565)
  5147. sensor.set_framesize(sensor.QVGA)
  5148. sensor.set_vflip(0)
  5149. sensor.set_hmirror(0)
  5150. sensor.run(1)
  5151. anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,6.92275, 6.718375, 9.01025) # anchor for face detect
  5152. dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)] # standard face key point position
  5153. a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
  5154. img_lcd = image.Image()
  5155. img_face = image.Image(size=(128, 128))
  5156. index = -1
  5157. a = img_face.pix_to_ai()
  5158. record_ftr = []
  5159. record_ftrs = []
  5160. gc.collect()
  5161. names = ` + value_class_input + `
  5162. `, true);
  5163. var code = variable_name + ` = sensor.snapshot()
  5164. code = kpu.run_yolo2(task_fd, ` + variable_name + `)
  5165. index = -1
  5166. if code:
  5167. for i in code:
  5168. # Cut face and resize to 128x128
  5169. a = ` + variable_name + `.draw_rectangle(i.rect())
  5170. face_cut = ` + variable_name + `.cut(i.x(), i.y(), i.w(), i.h())
  5171. face_cut_128 = face_cut.resize(128, 128)
  5172. a = face_cut_128.pix_to_ai()
  5173. #a = ` + variable_name + `.draw_image(face_cut_128, (0,0))
  5174. # Landmark for face 5 points
  5175. fmap = kpu.forward(task_ld, face_cut_128)
  5176. plist = fmap[:]
  5177. le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
  5178. re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
  5179. nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
  5180. lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
  5181. rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
  5182. a = ` + variable_name + `.draw_circle(le[0], le[1], 4)
  5183. a = ` + variable_name + `.draw_circle(re[0], re[1], 4)
  5184. a = ` + variable_name + `.draw_circle(nose[0], nose[1], 4)
  5185. a = ` + variable_name + `.draw_circle(lm[0], lm[1], 4)
  5186. a = ` + variable_name + `.draw_circle(rm[0], rm[1], 4)
  5187. # align face to standard position
  5188. src_point = [le, re, nose, lm, rm]
  5189. T = image.get_affine_transform(src_point, dst_point)
  5190. a = image.warp_affine_ai(` + variable_name + `, img_face, T)
  5191. a = img_face.ai_to_pix()
  5192. #a = ` + variable_name + `.draw_image(img_face, (128,0))
  5193. del(face_cut_128)
  5194. # calculate face feature vector
  5195. fmap = kpu.forward(task_fe, img_face)
  5196. feature = kpu.face_encode(fmap[:])
  5197. reg_flag = False
  5198. scores = []
  5199. for j in range(len(record_ftrs)):
  5200. score = kpu.face_compare(record_ftrs[j], feature)
  5201. scores.append(score)
  5202. max_score = 0
  5203. index = -1
  5204. for k in range(len(scores)):
  5205. if max_score < scores[k]:
  5206. max_score = scores[k]
  5207. index = k
  5208. if(len(names) > index):
  5209. if max_score > 85:
  5210. a = lcd_draw_string(` + variable_name + `, i.x(), i.y(), ("%s :%2.1f" % (names[index], max_score)), color=(0, 255, 0), scale=2, mono_space=False)
  5211. else:
  5212. a = lcd_draw_string(` + variable_name + `, i.x(), i.y(), ("X :%2.1f" % (max_score)), color=(255, 0, 0), scale=2, mono_space=False)
  5213. if start_processing:
  5214. record_ftr = feature
  5215. record_ftrs.append(record_ftr)
  5216. start_processing = False
  5217. `+ break_string +
  5218. variable_name + ` = ` + variable_name + `.cut(40,0,240,240)
  5219. a = lcd.display(` + variable_name + `, oft=(0,0))
  5220. gc.collect()
  5221. # kpu.memtest()
  5222. `;
  5223. return code;
  5224. };
  5225. Blockly.Blocks['ai_model_class'] = {
  5226. init: function () {
  5227. this.appendDummyInput()
  5228. .appendField(Blockly.Msg.ai_model_class);
  5229. this.appendValueInput("class_input")
  5230. .setCheck(null)
  5231. .appendField(Blockly.Msg.ai_model_class_name);
  5232. this.setPreviousStatement(true, null);
  5233. this.setNextStatement(true, null);
  5234. this.setColour("#ee783a");
  5235. this.setTooltip(Blockly.Msg.ai_model_class_TOOLTIP);
  5236. this.setHelpUrl("");
  5237. }
  5238. };
  5239. Blockly.Python['ai_model_class'] = function (block) {
  5240. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  5241. Blockly.Python.definitions_.import_modelface = `import KPU as kpu
  5242. import sensor
  5243. import lcd
  5244. from Maix import FPIOA, GPIO
  5245. from fpioa_manager import fm, board_info
  5246. import time
  5247. import gc
  5248. try:from cocorobo import display_cjk_string
  5249. except:pass
  5250. def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
  5251. try:
  5252. display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
  5253. return canvas
  5254. except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
  5255. Blockly.Python.addFunction("draw_string", `def draw_string(img, x, y, text, color, scale, bg=None ):
  5256. if bg:
  5257. img.draw_rectangle(x-2,y-2, len(text)*10*scale+4 , 24*scale, fill=True, color=bg)
  5258. image = lcd_draw_string(img, x, y, text, color=color,scale=scale*2,mono_space=False)
  5259. return image
  5260. `);
  5261. Blockly.Python.addVariable('class_', `############### config #################
  5262. class_names = ` + value_class_input + `
  5263. class_num = len(class_names)
  5264. sample_num = len(class_names) * 5
  5265. THRESHOLD = 11
  5266. board_cube = 0
  5267. button_state = False
  5268. ########################################
  5269. lcd.init(type=2)
  5270. lcd.rotation(1)
  5271. sensor.reset()
  5272. sensor.set_pixformat(sensor.RGB565)
  5273. sensor.set_framesize(sensor.QVGA)
  5274. sensor.set_vflip(0)
  5275. sensor.set_hmirror(0)
  5276. sensor.run(1)
  5277. #sensor.reset()
  5278. #sensor.set_pixformat(sensor.RGB565)
  5279. #sensor.set_framesize(sensor.QVGA)
  5280. #sensor.set_windowing((224, 224))
  5281. #if board_cube == 1:
  5282. # sensor.set_vflip(True)
  5283. # sensor.set_hmirror(True)
  5284. # lcd.init(type=2)
  5285. # lcd.rotation(2)
  5286. #else:
  5287. # lcd.init()
  5288. #fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
  5289. #key = GPIO(GPIO.GPIOHS0, GPIO.PULL_UP)
  5290. FPIOA().set_function(10, FPIOA.GPIO1)
  5291. key1 = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)
  5292. try:
  5293. del model
  5294. except Exception:
  5295. pass
  5296. try:
  5297. del classifier
  5298. except Exception:
  5299. pass
  5300. gc.collect()
  5301. model = kpu.load("/sd/preset/models/object_classifier.emodel")
  5302. classifier = kpu.classifier(model, class_num, sample_num)
  5303. cap_num = 0
  5304. train_status = 0
  5305. last_cap_time = 0
  5306. last_btn_status = 0
  5307. res_index = -1
  5308. `)
  5309. var code = `img = sensor.snapshot()
  5310. if board_cube:
  5311. img = img.rotation_corr(z_rotation=90)
  5312. img.pix_to_ai()
  5313. # capture img
  5314. if train_status == 0:
  5315. if key1.value() == 0 and last_btn_status == 1:
  5316. #time.sleep_ms(30)
  5317. #if key1.value() == 1 and (last_btn_status == 1) and (time.ticks_ms() - last_cap_time > 500):
  5318. last_btn_status = 0
  5319. last_cap_time = time.ticks_ms()
  5320. if cap_num < class_num:
  5321. index = classifier.add_class_img(img)
  5322. cap_num += 1
  5323. elif cap_num < class_num + sample_num:
  5324. index = classifier.add_sample_img(img)
  5325. cap_num += 1
  5326. #else:
  5327. # img = draw_string(img, 2, 200, "release boot key please", color=lcd.WHITE,scale=1, bg=lcd.RED)
  5328. else:
  5329. #time.sleep_ms(30)
  5330. if key1.value() == 1 and (last_btn_status == 0):
  5331. last_btn_status = 1
  5332. if cap_num < class_num:
  5333. img = draw_string(img, 0, 2, "press right key to cap "+class_names[cap_num], color=lcd.WHITE,scale=1, bg=lcd.RED)
  5334. elif cap_num < class_num + sample_num:
  5335. img = draw_string(img, 0, 2, "right key to cap sample{}".format(cap_num-class_num), color=lcd.WHITE,scale=1, bg=lcd.RED)
  5336. # train and predict
  5337. if train_status == 0:
  5338. if cap_num >= class_num + sample_num:
  5339. img = draw_string(img, 30, 100, "training...", color=lcd.WHITE,scale=1, bg=lcd.RED)
  5340. lcd.display(img)
  5341. classifier.train()
  5342. train_status = 1
  5343. else:
  5344. res_index = -1
  5345. try:
  5346. res_index, min_dist = classifier.predict(img)
  5347. except Exception as e:
  5348. print("predict err:", e)
  5349. if res_index >= 0 and min_dist < THRESHOLD :
  5350. img = draw_string(img, 2, 2, class_names[res_index], color=lcd.WHITE,scale=1, bg=lcd.RED)
  5351. else:
  5352. img = draw_string(img, 2, 2, 'maybe {}'.format(class_names[res_index]), color=lcd.WHITE,scale=1, bg=lcd.RED)
  5353. img = img.cut(0,0,240,240)
  5354. lcd.display(img, oft=(0,0))
  5355. `;
  5356. return code;
  5357. };
  5358. Blockly.Blocks['ai_model_getface'] = {
  5359. init: function () {
  5360. this.appendDummyInput()
  5361. .appendField(Blockly.Msg.ai_model_getface);
  5362. this.setOutput(true, null);
  5363. this.setColour("#ee783a");
  5364. this.setTooltip(Blockly.Msg.ai_model_getface_TOOLTIP);
  5365. this.setHelpUrl("");
  5366. }
  5367. };
  5368. Blockly.Python['ai_model_getface'] = function (block) {
  5369. // TODO: Assemble Python into code variable.
  5370. var code = 'names[index] if (index != -1 and len(names) > index) else ""';
  5371. // TODO: Change ORDER_NONE to the correct strength.
  5372. return [code, Blockly.Python.ORDER_NONE];
  5373. };
  5374. Blockly.Blocks['ai_model_getclass'] = {
  5375. init: function () {
  5376. this.appendDummyInput()
  5377. .appendField(Blockly.Msg.ai_model_getclass);
  5378. this.setOutput(true, null);
  5379. this.setColour("#ee783a");
  5380. this.setTooltip(Blockly.Msg.ai_model_getclass_TOOLTIP);
  5381. this.setHelpUrl("");
  5382. }
  5383. };
  5384. Blockly.Python['ai_model_getclass'] = function (block) {
  5385. // TODO: Assemble Python into code variable.
  5386. var code = 'class_names[res_index] if (res_index != -1 and len(class_names) > res_index) else ""';
  5387. // TODO: Change ORDER_NONE to the correct strength.
  5388. return [code, Blockly.Python.ORDER_NONE];
  5389. };
  5390. Blockly.Blocks['ai_model_customized_if_got'] = {
  5391. init: function () {
  5392. this.appendDummyInput()
  5393. .appendField(Blockly.Msg.ai_models_customized_if_got);
  5394. this.setOutput(true, null);
  5395. this.setColour("#ee783a");
  5396. this.setTooltip(Blockly.Msg.ai_model_customized_if_got_TOOLTIP);
  5397. this.setHelpUrl("");
  5398. }
  5399. };
  5400. Blockly.Python['ai_model_customized_if_got'] = function (block) {
  5401. // TODO: Assemble Python into code variable.
  5402. var code = 'code_customized_model';
  5403. // TODO: Change ORDER_NONE to the correct strength.
  5404. return [code, Blockly.Python.ORDER_NONE];
  5405. };
  5406. Blockly.Blocks['ai_model_customized_get_result'] = {
  5407. init: function () {
  5408. this.appendDummyInput()
  5409. .appendField(Blockly.Msg.ai_models_from)
  5410. .appendField(new Blockly.FieldVariable("i"), "varitem")
  5411. .appendField(Blockly.Msg.ai_models_customized_get_result_any)
  5412. .appendField(new Blockly.FieldDropdown([
  5413. [Blockly.Msg.ai_models_customized_get_result_name, "classid"],
  5414. [Blockly.Msg.ai_models_customized_get_result_x, "x"],
  5415. [Blockly.Msg.ai_models_customized_get_result_y, "y"],
  5416. [Blockly.Msg.ai_models_customized_get_result_w, "w"],
  5417. [Blockly.Msg.ai_models_customized_get_result_h, "h"],
  5418. [Blockly.Msg.ai_models_customized_get_result_x_center, "cx"],
  5419. [Blockly.Msg.ai_models_customized_get_result_y_center, "cy"],
  5420. [Blockly.Msg.ai_models_customized_get_result_count, "objnum"],
  5421. [Blockly.Msg.ai_models_customized_get_result_index, "index"],
  5422. [Blockly.Msg.ai_models_customized_get_result_confidence, "value"]
  5423. ]), "get_results");
  5424. this.setOutput(true, null);
  5425. this.setColour("#ee783a");
  5426. var thisBlock = this;
  5427. this.setTooltip(function () {
  5428. var mode = thisBlock.getFieldValue('get_results');
  5429. var TOOLTIPS = {
  5430. 'classid': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_name),
  5431. 'x': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x),
  5432. 'y': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y),
  5433. 'w': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_w),
  5434. 'h': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_h),
  5435. 'cx': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x_center),
  5436. 'cy': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y_center),
  5437. 'objnum': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_count),
  5438. 'index': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_index),
  5439. 'value': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_confidence)
  5440. };
  5441. return TOOLTIPS[mode];
  5442. });
  5443. this.setHelpUrl("");
  5444. }
  5445. };
  5446. Blockly.Python['ai_model_customized_get_result'] = function (block) {
  5447. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5448. var dropdown_get_results = block.getFieldValue('get_results');
  5449. // TODO: Assemble Python into code variable.
  5450. if (dropdown_get_results == "classid") {
  5451. var code = 'classes_customized_model[' + variable_name + '.classid()]';
  5452. } else if ((dropdown_get_results != "classid") && (dropdown_get_results != "cx") && (dropdown_get_results != "cy")) {
  5453. var code = variable_name + '.' + dropdown_get_results + '()';
  5454. } else if (dropdown_get_results == "cx") {
  5455. var code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  5456. } else if (dropdown_get_results == "cy") {
  5457. var code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  5458. }
  5459. // TODO: Change ORDER_NONE to the correct strength.
  5460. return [code, Blockly.Python.ORDER_NONE];
  5461. };
  5462. /*
  5463. Blockly.Blocks['ai_model_init'] = {
  5464. init: function() {
  5465. this.appendDummyInput()
  5466. .appendField(Blockly.Msg.MODELLPM)
  5467. .appendField(new Blockly.FieldDropdown([
  5468. ["Single Digit Recognition", "Single Digit Recognition"],
  5469. ["Common Object Recognition", "Common Object Recognition"],
  5470. ["Animal Recognition", "Animal Recognition"],
  5471. ["Human Face Recognition", "Human Face Recognition"]
  5472. ]), "DEGREE")
  5473. this.setPreviousStatement(true, null);
  5474. this.setNextStatement(true, null);
  5475. this.setColour("#ee783a");
  5476. this.setTooltip('');
  5477. this.setHelpUrl('');
  5478. }
  5479. };
  5480. Blockly.Python.ai_model_init = function(block) {
  5481. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  5482. var degee = block.getFieldValue('DEGREE');
  5483. var _code = ""
  5484. if (degee == "Single Digit Recognition") {
  5485. _code += "task_mnist = kpu.load(\"/sd/models/mnist.kmodel\")\n";
  5486. } else if (degee == "Common Object Recognition") {
  5487. _code += "classes_animalrecognition = ['aeroplane', 'bicycle', '', 'boat', 'bottle', 'bus', 'car', '', 'chair', '', 'diningtable', '', '', 'motorbike', 'person', 'pottedplant', '', 'sofa', 'train', 'tvmonitor']\n";
  5488. _code += "task_animalrecognition = kpu.load(\"/sd/models/object-recognition.kmodel\")\n";
  5489. _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
  5490. _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
  5491. } else if (degee == "Animal Recognition") {
  5492. _code += "classes_animalrecognition = ['', '', 'bird', '', '', '', '', 'cat', '', 'cow', '', 'dog', 'horse', '', '', '', 'sheep', '', '', '']\n";
  5493. _code += "task_animalrecognition = kpu.load(\"/sd/models/animal-recognition.kmodel\")\n";
  5494. _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
  5495. _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
  5496. } else {
  5497. _code += "task_facerecognition = kpu.load(\"/sd/models/face-recognition.kmodel\")\n";
  5498. _code += "anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n";
  5499. _code += "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor)\n";
  5500. }
  5501. return _code;
  5502. }
  5503. Blockly.Blocks['ai_model_img_mnist'] = {
  5504. init: function() {
  5505. this.appendDummyInput()
  5506. .appendField(Blockly.Msg.CAMERINIT);
  5507. this.appendDummyInput()
  5508. .appendField(Blockly.Msg.CAMERINIT_SISF)
  5509. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  5510. this.setPreviousStatement(true, null);
  5511. this.setNextStatement(true, null);
  5512. this.setColour("#ee783a");
  5513. this.setTooltip('');
  5514. this.setHelpUrl('');
  5515. }
  5516. };
  5517. Blockly.Python.ai_model_img_mnist = function(block) {
  5518. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5519. var _code = "img_mnist1=" + variable_name + ".to_grayscale(1)\n"
  5520. _code += "img_mnist2=img_mnist1.resize(28,28)\n"
  5521. _code += "a=img_mnist2.invert()\n"
  5522. _code += "a=img_mnist2.strech_char(1)\n"
  5523. _code += "a=img_mnist2.pix_to_ai()\n"
  5524. _code += "fmap_mnist=kpu.forward(task_mnist,img_mnist2)\n"
  5525. _code += "plist_mnist=fmap_mnist[:]\n"
  5526. _code += "pmax_mnist=max(plist_mnist)\n"
  5527. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n"
  5528. return _code;
  5529. }
  5530. */
  5531. /*
  5532. Blockly.Blocks['ai_model_pmax_mnist'] = {
  5533. init: function() {
  5534. this.appendDummyInput()
  5535. .appendField(Blockly.Msg.CAMERINIT_FPM)
  5536. this.appendDummyInput()
  5537. .appendField(Blockly.Msg.CAMERINIT_GTCS)
  5538. this.setOutput(true, null);
  5539. this.setColour("#ee783a");
  5540. this.setTooltip('');
  5541. this.setHelpUrl('');
  5542. }
  5543. };
  5544. Blockly.Python.ai_model_pmax_mnist = function(block) {
  5545. var _code = "int(pmax_mnist*100)\n"
  5546. return _code;
  5547. }
  5548. Blockly.Blocks['ai_model_objectrecognition'] = {
  5549. init: function() {
  5550. this.appendDummyInput()
  5551. .appendField(Blockly.Msg.CAMERINITPM)
  5552. this.appendDummyInput()
  5553. .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
  5554. .appendField(new Blockly.FieldVariable("img_objectrecognition"), "varitem");
  5555. this.setPreviousStatement(true, null);
  5556. this.setNextStatement(true, null);
  5557. this.setColour("#ee783a");
  5558. this.setTooltip('');
  5559. this.setHelpUrl('');
  5560. }
  5561. };
  5562. Blockly.Python.ai_model_objectrecognition = function(block) {
  5563. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5564. var _code = "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n"
  5565. return _code;
  5566. }
  5567. Blockly.Blocks['ai_model_aobjectrecognition'] = {
  5568. init: function() {
  5569. this.appendDummyInput()
  5570. .appendField(Blockly.Msg.CAMERINITPM_ACOID)
  5571. this.setOutput(true, null);
  5572. this.setColour("#ee783a");
  5573. this.setTooltip('');
  5574. this.setHelpUrl('');
  5575. }
  5576. };
  5577. Blockly.Python.ai_model_aobjectrecognition = function(block) {
  5578. var _code = "code_objectrecognition\n"
  5579. return _code;
  5580. }
  5581. Blockly.Blocks['ai_model_ifaobjectrecognition'] = {
  5582. init: function() {
  5583. this.appendDummyInput()
  5584. .appendField(Blockly.Msg.OLCD_SETROTATION)
  5585. .appendField(new Blockly.FieldDropdown([
  5586. ["0°", "1"],
  5587. ["90°", "2"],
  5588. ["180°", "3"],
  5589. ["270°", "0"]
  5590. ]), "DEGREE")
  5591. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  5592. this.setPreviousStatement(true, null);
  5593. this.setNextStatement(true, null);
  5594. this.setColour("#ee783a");
  5595. this.setTooltip('');
  5596. this.setHelpUrl('');
  5597. }
  5598. };
  5599. Blockly.Python.ai_model_ifaobjectrecognition = function(block) {
  5600. // if code_objectrecognition: # if object is detected then do
  5601. // for i_objectrecognition in code_objectrecognition:
  5602. // # ...
  5603. var _code = "code_objectrecognition\n"
  5604. return _code;
  5605. }
  5606. Blockly.Blocks['ai_model_xyobjectreocgnition'] = {
  5607. init: function() {
  5608. this.appendDummyInput()
  5609. .appendField(Blockly.Msg.CAMERINITPM_FPM)
  5610. this.appendDummyInput()
  5611. .appendField(Blockly.Msg.CAMERINITPM_G)
  5612. .appendField(new Blockly.FieldDropdown([
  5613. ["X-Coordinate", "x"],
  5614. ["Y-Coordinate", "y"],
  5615. ["Width of the Detection Box", "w"],
  5616. ["Height of the Detection Box", "h"],
  5617. ["Number of Objects Detected", "objnum"],
  5618. ["Index of Object Detected", "index"],
  5619. ["Confidence Score (%)", "value"]
  5620. ]), "DEGREE")
  5621. .appendField(Blockly.Msg.CAMERINITPM_GIOTCOD)
  5622. this.setOutput(true, null);
  5623. this.setColour("#ee783a");
  5624. this.setTooltip('');
  5625. this.setHelpUrl('');
  5626. }
  5627. };
  5628. Blockly.Python.ai_model_xyobjectreocgnition = function(block) {
  5629. var degee = block.getFieldValue('DEGREE');
  5630. var _code = "i_objectreocgnition." + degee + "()\n"
  5631. return _code;
  5632. }
  5633. Blockly.Blocks['ai_model_cobjectrecognition'] = {
  5634. init: function() {
  5635. this.appendDummyInput()
  5636. .appendField(Blockly.Msg.CAMERINITPM_DCOIA)
  5637. .appendField(new Blockly.FieldDropdown([
  5638. ["Aeroplane", "Aeroplane"],
  5639. ["Bicycle", "Bicycle"],
  5640. ["Boat", "Boat"],
  5641. ["Bus", "Bus"],
  5642. ["Car", "Car"],
  5643. ["Chair", "Chair"],
  5644. ["Dining", "Dining"],
  5645. ["Table", "Table"],
  5646. ["Motorbike", "Motorbike"],
  5647. ["Person", "Person"],
  5648. ["Potted", "Potted"],
  5649. ["Plant", "Plant"],
  5650. ["Sofa", "Sofa"],
  5651. ["Train", "Train"],
  5652. ["Monitor", "Monitor"]
  5653. ]), "DEGREE")
  5654. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  5655. this.setOutput(true, null);
  5656. this.setColour("#ee783a");
  5657. this.setTooltip('');
  5658. this.setHelpUrl('');
  5659. }
  5660. };
  5661. Blockly.Python.ai_model_cobjectrecognition = function(block) {
  5662. var degee = block.getFieldValue('DEGREE');
  5663. var _code = "classes_objectrecognition[i.classid()]\n"
  5664. return _code;
  5665. }
  5666. Blockly.Blocks['ai_model_animalrecognition'] = {
  5667. init: function() {
  5668. this.appendDummyInput()
  5669. .appendField(Blockly.Msg.CAMERINITPMA)
  5670. this.appendDummyInput()
  5671. .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
  5672. .appendField(new Blockly.FieldVariable("img_animalrecognition"), "varitem");
  5673. this.setPreviousStatement(true, null);
  5674. this.setNextStatement(true, null);
  5675. this.setColour("#ee783a");
  5676. this.setTooltip('');
  5677. this.setHelpUrl('');
  5678. }
  5679. };
  5680. Blockly.Python.ai_model_animalrecognition = function(block) {
  5681. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5682. var _code = "code_animalrecognition = kpu.run_yolo2(task_animalrecognition, " + variable_name + ")\n"
  5683. return _code;
  5684. }
  5685. Blockly.Blocks['ai_model_aanimalrecognition'] = {
  5686. init: function() {
  5687. this.appendDummyInput()
  5688. .appendField(Blockly.Msg.CAMERINITPMA_AAOID)
  5689. this.setOutput(true, null);
  5690. this.setColour("#ee783a");
  5691. this.setTooltip('');
  5692. this.setHelpUrl('');
  5693. }
  5694. };
  5695. Blockly.Python.ai_model_aanimalrecognition = function(block) {
  5696. var _code = "code_animalrecognition\n"
  5697. return _code;
  5698. }
  5699. Blockly.Blocks['ai_model_ifanimalrecognition'] = {
  5700. init: function() {
  5701. this.appendDummyInput()
  5702. .appendField(Blockly.Msg.OLCD_SETROTATION)
  5703. .appendField(new Blockly.FieldDropdown([
  5704. ["0°", "1"],
  5705. ["90°", "2"],
  5706. ["180°", "3"],
  5707. ["270°", "0"]
  5708. ]), "DEGREE")
  5709. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  5710. this.setPreviousStatement(true, null);
  5711. this.setNextStatement(true, null);
  5712. this.setColour("#ee783a");
  5713. this.setTooltip('');
  5714. this.setHelpUrl('');
  5715. }
  5716. };
  5717. Blockly.Python.ai_model_ifanimalrecognition = function(block) {
  5718. // if code_animalrecognition: # if object is detected then do
  5719. // for i_animalrecognition in code_animalrecognition:
  5720. // # ...
  5721. var _code = "code_objectrecognition\n"
  5722. return _code;
  5723. }
  5724. Blockly.Blocks['ai_model_xyanimalrecognition'] = {
  5725. init: function() {
  5726. this.appendDummyInput()
  5727. .appendField(Blockly.Msg.CAMERINITPMA_FPMA)
  5728. this.appendDummyInput()
  5729. .appendField(Blockly.Msg.CAMERINITPM_G)
  5730. .appendField(new Blockly.FieldDropdown([
  5731. ["X-Coordinate", "x"],
  5732. ["Y-Coordinate", "y"],
  5733. ["Width of the Detection Box", "w"],
  5734. ["Height of the Detection Box", "h"],
  5735. ["Number of Objects Detected", "objnum"],
  5736. ["Index of Object Detected", "index"],
  5737. ["Confidence Score (%)", "value"]
  5738. ]), "DEGREE")
  5739. .appendField(Blockly.Msg.CAMERINITPMA_GIOTCAD)
  5740. this.setOutput(true, null);
  5741. this.setColour("#ee783a");
  5742. this.setTooltip('');
  5743. this.setHelpUrl('');
  5744. }
  5745. };
  5746. Blockly.Python.ai_model_xyanimalrecognition = function(block) {
  5747. var degee = block.getFieldValue('DEGREE');
  5748. var _code = "i_animalrecognition." + degee + "()\n"
  5749. return _code;
  5750. }
  5751. Blockly.Blocks['ai_model_canimalrecognition'] = {
  5752. init: function() {
  5753. this.appendDummyInput()
  5754. .appendField(Blockly.Msg.CAMERINITPMA_DAOIA)
  5755. .appendField(new Blockly.FieldDropdown([
  5756. ["Bird", "Bird"],
  5757. ["Cat", "Cat"],
  5758. ["Cow", "Cow"],
  5759. ["Dog", "Dog"],
  5760. ["Horse", "Horse"],
  5761. ["Sheep", "Sheep"]
  5762. ]), "DEGREE")
  5763. this.setOutput(true, null);
  5764. this.setColour("#ee783a");
  5765. this.setTooltip('');
  5766. this.setHelpUrl('');
  5767. }
  5768. };
  5769. Blockly.Python.ai_model_canimalrecognition = function(block) {
  5770. var degee = block.getFieldValue('DEGREE');
  5771. var _code = "classes_animalrecognition[i.classid()]\n"
  5772. return _code;
  5773. }
  5774. Blockly.Blocks['ai_model_facerecognition'] = {
  5775. init: function() {
  5776. this.appendDummyInput()
  5777. .appendField(Blockly.Msg.CAMERINITPMF)
  5778. this.appendDummyInput()
  5779. .appendField(Blockly.Msg.CAMERINITPMF_SISFIF)
  5780. .appendField(new Blockly.FieldVariable("img_facerecognition"), "varitem");
  5781. this.setPreviousStatement(true, null);
  5782. this.setNextStatement(true, null);
  5783. this.setColour("#ee783a");
  5784. this.setTooltip('');
  5785. this.setHelpUrl('');
  5786. }
  5787. };
  5788. Blockly.Python.ai_model_facerecognition = function(block) {
  5789. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5790. var _code = "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n"
  5791. return _code;
  5792. }
  5793. Blockly.Blocks['ai_model_afacerecognition'] = {
  5794. init: function() {
  5795. this.appendDummyInput()
  5796. .appendField(Blockly.Msg.CAMERINITPMF_AFID)
  5797. this.setOutput(true, null);
  5798. this.setColour("#ee783a");
  5799. this.setTooltip('');
  5800. this.setHelpUrl('');
  5801. }
  5802. };
  5803. Blockly.Python.ai_model_afacerecognition = function(block) {
  5804. var _code = "code_facerecognition\n"
  5805. return _code;
  5806. }
  5807. Blockly.Blocks['ai_model_iffacerecognition'] = {
  5808. init: function() {
  5809. this.appendDummyInput()
  5810. .appendField(Blockly.Msg.OLCD_SETROTATION)
  5811. .appendField(new Blockly.FieldDropdown([
  5812. ["0°", "1"],
  5813. ["90°", "2"],
  5814. ["180°", "3"],
  5815. ["270°", "0"]
  5816. ]), "DEGREE")
  5817. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  5818. this.setPreviousStatement(true, null);
  5819. this.setNextStatement(true, null);
  5820. this.setColour("#ee783a");
  5821. this.setTooltip('');
  5822. this.setHelpUrl('');
  5823. }
  5824. };
  5825. Blockly.Python.ai_model_iffacerecognition = function(block) {
  5826. // if code_facerecognition: # if face is detected then do
  5827. // for i_facerecognition in code_facerecognition:
  5828. // # ...
  5829. var _code = "code_objectrecognition\n"
  5830. return _code;
  5831. }
  5832. Blockly.Blocks['ai_model_xyfacerecognition'] = {
  5833. init: function() {
  5834. this.appendDummyInput()
  5835. .appendField(Blockly.Msg.CAMERINITPMF_FPM)
  5836. this.appendDummyInput()
  5837. .appendField(Blockly.Msg.CAMERINITPM_G)
  5838. .appendField(new Blockly.FieldDropdown([
  5839. ["X-Coordinate", "x"],
  5840. ["Y-Coordinate", "y"],
  5841. ["Width of the Detection Box", "w"],
  5842. ["Height of the Detection Box", "h"],
  5843. ["Number of Objects Detected", "objnum"],
  5844. ["Index of Object Detected", "index"],
  5845. ["Confidence Score (%)", "value"]
  5846. ]), "DEGREE")
  5847. .appendField(Blockly.Msg.CAMERINITPMF_GIOTFD)
  5848. this.setOutput(true, null);
  5849. this.setColour("#ee783a");
  5850. this.setTooltip('');
  5851. this.setHelpUrl('');
  5852. }
  5853. };
  5854. Blockly.Python.ai_model_xyfacerecognition = function(block) {
  5855. var degee = block.getFieldValue('DEGREE');
  5856. var _code = "i_facerecognition." + degee + "()\n"
  5857. return _code;
  5858. }
  5859. Blockly.Blocks['ai_model_kpuload'] = {
  5860. init: function() {
  5861. this.appendDummyInput()
  5862. .appendField(Blockly.Msg.CAMERINITPMF_LCMLM);
  5863. this.appendDummyInput()
  5864. .appendField(Blockly.Msg.CAMERINITPMF_FSC)
  5865. .appendField(new Blockly.FieldTextInput("/sd/models/facedetect.kmodel"), "input")
  5866. this.setOutput(true, null);
  5867. this.setColour("#ee783a");
  5868. this.setTooltip('');
  5869. this.setHelpUrl('');
  5870. }
  5871. };
  5872. Blockly.Python.ai_model_kpuload = function(block) {
  5873. var input = block.getFieldValue('input');
  5874. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  5875. var _code = "kpu.load(\"" + input + "\")\n"
  5876. return _code;
  5877. }
  5878. Blockly.Blocks['ai_model_inityolo2'] = {
  5879. init: function() {
  5880. this.appendDummyInput()
  5881. .appendField(Blockly.Msg.CAMERINIINNYFNOT)
  5882. .appendField(Blockly.Msg.CAMERINIINNYFNOT_F)
  5883. .appendField(new Blockly.FieldVariable("task"), "varitem");
  5884. this.appendDummyInput()
  5885. .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT)
  5886. .appendField(new Blockly.FieldTextInput("0"), "input")
  5887. .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT_S)
  5888. .appendField(new Blockly.FieldTextInput("0"), "input1");
  5889. this.appendDummyInput()
  5890. .appendField(Blockly.Msg.CAMERINITPMF_SNOAA)
  5891. .appendField(new Blockly.FieldTextInput("5"), "input2")
  5892. .appendField(Blockly.Msg.CAMERINITPMF_SNOAA_S)
  5893. .appendField(new Blockly.FieldVariable("anchor"), "varitem1");
  5894. this.setPreviousStatement(true, null);
  5895. this.setNextStatement(true, null);
  5896. this.setColour("#ee783a");
  5897. this.setTooltip('');
  5898. this.setHelpUrl('');
  5899. }
  5900. };
  5901. Blockly.Python.ai_model_inityolo2 = function(block) {
  5902. var varitem = block.getFieldValue('varitem');
  5903. var varitem1 = block.getFieldValue('varitem1');
  5904. var input = block.getFieldValue('input');
  5905. var input1 = block.getFieldValue('input1');
  5906. var input2 = block.getFieldValue('input2');
  5907. var _code = "kpu.init_yolo2(" + varitem + ", " + input + ", " + input1 + ", " + input2 + ", " + varitem1 + ")\n"
  5908. return _code;
  5909. }
  5910. Blockly.Blocks['ai_model_runyolo2'] = {
  5911. init: function() {
  5912. this.appendDummyInput()
  5913. .appendField(Blockly.Msg.CAMERGRFRNNY);
  5914. this.appendDummyInput()
  5915. .appendField(Blockly.Msg.CAMERGRFRNNY_FNOT)
  5916. .appendField(new Blockly.FieldVariable("task"), "varitem");
  5917. this.appendDummyInput()
  5918. .appendField(Blockly.Msg.CAMERGRFRNNY_SISA)
  5919. .appendField(new Blockly.FieldVariable("camera"), "varitem1");
  5920. this.setOutput(true, null);
  5921. this.setColour("#ee783a");
  5922. this.setTooltip('');
  5923. this.setHelpUrl('');
  5924. }
  5925. };
  5926. Blockly.Python.ai_model_runyolo2 = function(block) {
  5927. var varitem = block.getFieldValue('varitem');
  5928. var varitem1 = block.getFieldValue('varitem1');
  5929. var _code = "kpu.run_yolo2(" + varitem + ", " + varitem1 + ")\n"
  5930. return _code;
  5931. }
  5932. Blockly.Blocks['ai_model_forward'] = {
  5933. init: function() {
  5934. this.appendDummyInput()
  5935. .appendField(Blockly.Msg.CAMERGFMOFNOT)
  5936. .appendField(new Blockly.FieldVariable("task"), "varitem")
  5937. this.appendDummyInput()
  5938. .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
  5939. .appendField(new Blockly.FieldTextInput("3"), "input")
  5940. this.appendDummyInput()
  5941. .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
  5942. .appendField(new Blockly.FieldVariable("task"), "varitem1")
  5943. this.setOutput(true, null);
  5944. this.setColour("#ee783a");
  5945. this.setTooltip('');
  5946. this.setHelpUrl('');
  5947. }
  5948. };
  5949. Blockly.Python.ai_model_forward = function(block) {
  5950. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5951. var variable_name1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
  5952. var input = block.getFieldValue('input');
  5953. var _code = "kpu.forward(" + variable_name + "," + variable_name1 + "," + input + ")\n"
  5954. return _code;
  5955. }
  5956. Blockly.Blocks['ai_model_deinit'] = {
  5957. init: function() {
  5958. this.appendDummyInput()
  5959. .appendField(Blockly.Msg.CAMERDNOT)
  5960. .appendField(new Blockly.FieldVariable("task"), "varitem")
  5961. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  5962. this.setPreviousStatement(true, null);
  5963. this.setNextStatement(true, null);
  5964. this.setColour("#ee783a");
  5965. this.setTooltip('');
  5966. this.setHelpUrl('');
  5967. }
  5968. };
  5969. Blockly.Python.ai_model_deinit = function(block) {
  5970. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  5971. var _code = "kpu.deinit(" + variable_name + ")\n"
  5972. return _code;
  5973. }
  5974. Blockly.Blocks['ai_vision_if'] = {
  5975. init: function() {
  5976. this.appendDummyInput()
  5977. .appendField(Blockly.Msg.VISION_IF)
  5978. .appendField(new Blockly.FieldDropdown([
  5979. ["Red", "fcr_threshold_red"],
  5980. ["Green", "fcr_threshold_green"],
  5981. ["Blue", "fcr_threshold_blue"]
  5982. ]), "color")
  5983. .appendField(Blockly.Msg.VISION_IF_R)
  5984. .appendField(new Blockly.FieldVariable("img"), "varitem");
  5985. this.appendDummyInput()
  5986. .appendField(Blockly.Msg.VISION_IF_S)
  5987. .appendField(new Blockly.FieldTextInput("60"), "input")
  5988. .appendField(Blockly.Msg.HID_MOUSE_MOVE_DISTANCE);
  5989. this.appendStatementInput('DO0')
  5990. .appendField(Blockly.Msg.TOUCH_DO);
  5991. this.setPreviousStatement(true);
  5992. this.setNextStatement(true);
  5993. this.setColour("#f2a247");
  5994. }
  5995. };
  5996. Blockly.Python.ai_vision_if = function(block) {
  5997. var input = block.getFieldValue('input');
  5998. var varitem = block.getFieldValue('varitem');
  5999. var color = block.getFieldValue('color');
  6000. Blockly.Python.addVariable('fcr_threshold_red', 'fcr_threshold_red = (30, 100, 15, 127, 15, 127)', true);
  6001. Blockly.Python.addVariable('fcr_threshold_green', 'fcr_threshold_green = (30, 100, -64, -8, -32, 32)', true);
  6002. Blockly.Python.addVariable('fcr_threshold_blue', 'fcr_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
  6003. Blockly.Python.addVariable('fcr_current_threshold', 'fcr_current_threshold = ' + color, true);
  6004. Blockly.Python.addVariable('fcr_max_region_size_detected', 'fcr_max_region_size_detected = ' + input, true);
  6005. var _code = "fcr_blobs = " + varitem + ".find_blobs([fcr_current_threshold], area_threshold=150)\n"
  6006. _code += " if fcr_blobs: \n"
  6007. _code += " for b in fcr_blobs:\n"
  6008. _code += " if (b[2] > fcr_max_region_size_detected) or ((b[3] > fcr_max_region_size_detected)):\n"
  6009. _code += " " + varitem + ".draw_rectangle(b[0:4])\n"
  6010. _code += " " + varitem + ".draw_cross(b[5], b[6])\n"
  6011. _code += " " + varitem + ".draw_rectangle(b[0], b[1]-20, b[2], 20, color=(255,255,255), fill=True)\n"
  6012. _code += " " + varitem + ".draw_string(b[0]+2, b[1]-20+2, \"x:\"+ str(b[0]) + \", y:\"+ str(b[1]), mono_space=False, color=(0,0,0), scale=2)\n"
  6013. return _code;
  6014. }
  6015. Blockly.Blocks['ai_vision_gd'] = {
  6016. init: function() {
  6017. this.appendDummyInput()
  6018. .appendField(Blockly.Msg.VISION_IF_C)
  6019. .appendField(new Blockly.FieldDropdown([
  6020. ["X Coordinate", "0"],
  6021. ["Y Coordinate", "1"],
  6022. ["Width", "2"],
  6023. ["Height", "3"],
  6024. ]), "dropdown");
  6025. this.setOutput(true, null);
  6026. this.setColour("#f2a247");
  6027. this.setTooltip("");
  6028. this.setHelpUrl("");
  6029. }
  6030. };
  6031. Blockly.Python.ai_vision_gd = function(block) {
  6032. var d = block.getFieldValue('dropdown');
  6033. var _code = "b[" + d + "]\n"
  6034. return _code;
  6035. }
  6036. Blockly.Blocks['ai_vision_dif'] = {
  6037. init: function() {
  6038. this.appendDummyInput()
  6039. .appendField(Blockly.Msg.CONTROLS_IF_MSG_IF)
  6040. .appendField(new Blockly.FieldDropdown([
  6041. ["Red", "fcr_threshold_red"],
  6042. ["Green", "fcr_threshold_green"],
  6043. ["Blue", "fcr_threshold_blue"]
  6044. ]), "color")
  6045. .appendField(Blockly.Msg.VISION_CIF)
  6046. .appendField(new Blockly.FieldVariable("img"), "varitem");
  6047. this.appendStatementInput('DO0')
  6048. .appendField(Blockly.Msg.TOUCH_DO);
  6049. this.setPreviousStatement(true);
  6050. this.setNextStatement(true);
  6051. this.setColour("#f2a247");
  6052. }
  6053. };
  6054. Blockly.Python.ai_vision_dif = function(block) {
  6055. var color = block.getFieldValue('color');
  6056. var varitem = block.getFieldValue('varitem');
  6057. Blockly.Python.addVariable('fc_threshold_red', 'fc_threshold_red = (30, 100, 15, 127, 15, 127)', true);
  6058. Blockly.Python.addVariable('fc_threshold_green', 'fc_threshold_green = (30, 100, -64, -8, -32, 32)', true);
  6059. Blockly.Python.addVariable('fc_threshold_blue', 'fc_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
  6060. Blockly.Python.addVariable('fc_color_threshold', 'fc_color_threshold = ' + color, true);
  6061. Blockly.Python.addVariable('fc_bounding_box_color', 'fc_bounding_box_color = (200,200,200)', true);
  6062. var _code = "def fc_expand_roi(roi):\n"
  6063. _code += " extra = 5\n"
  6064. _code += " win_size = (224, 224)\n"
  6065. _code += " (x, y, width, height) = roi\n"
  6066. _code += " fc_new_roi = [x-extra, y-extra, width+2*extra, height+2*extra]\n"
  6067. _code += "\n"
  6068. _code += " if fc_new_roi[0] < 0:\n"
  6069. _code += " fc_new_roi[0] = 0\n"
  6070. _code += " if fc_new_roi[1] < 0:\n"
  6071. _code += " fc_new_roi[1] = 0\n"
  6072. _code += " if fc_new_roi[2] > win_size[0]:\n"
  6073. _code += " fc_new_roi[2] = win_size[0]\n"
  6074. _code += " if fc_new_roi[3] > win_size[1]:\n"
  6075. _code += " fc_new_roi[3] = win_size[1]\n"
  6076. _code += "\n"
  6077. _code += " return tuple(fc_new_roi)\n"
  6078. Blockly.Python.addFunction("fc_expand_roi", _code);
  6079. _code = "fc_blobs = " + varitem + ".find_blobs([fc_color_threshold], area_threshold=150)\n"
  6080. " if fc_blobs:\n"
  6081. " for fc_blob in fc_blobs:\n"
  6082. " fc_is_circle = False\n"
  6083. " fc_max_circle = None\n"
  6084. " fc_max_radius = -1\n"
  6085. " fc_new_roi = fc_expand_roi(fc_blob.rect())\n"
  6086. "\n"
  6087. " for fc_c in " + varitem + ".find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=fc_new_roi):\n"
  6088. " fc_is_circle = True\n"
  6089. " # " + varitem + ".draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255))\n"
  6090. " if fc_c.r() > fc_max_radius:\n"
  6091. " fc_max_radius = fc_c.r()\n"
  6092. " fc_max_circle = fc_c\n"
  6093. " if fc_is_circle:\n"
  6094. " " + varitem + ".draw_rectangle(fc_new_roi, thickness=1)\n"
  6095. " " + varitem + ".draw_rectangle(fc_new_roi[0], fc_new_roi[1]-20, fc_new_roi[2], 20, color=(255,255,255), fill=True)\n"
  6096. " " + varitem + ".draw_string(fc_new_roi[0]+2, fc_new_roi[1]-20+2, \"x:\"+ str(fc_new_roi[0]) + \", y:\"+ str(fc_new_roi[1]), mono_space=False, color=(0,0,0), scale=2)\n"
  6097. " " + varitem + ".draw_cross(fc_blob[5], fc_blob[6])\n"
  6098. " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r(), color = fc_bounding_box_color)\n"
  6099. " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r() + 1, color = fc_bounding_box_color)\n"
  6100. return _code;
  6101. }
  6102. Blockly.Blocks['ai_vision_dgd'] = {
  6103. init: function() {
  6104. this.appendDummyInput()
  6105. .appendField(Blockly.Msg.VISION_CIF_D)
  6106. .appendField(new Blockly.FieldDropdown([
  6107. ["X Coordinate", "0"],
  6108. ["Y Coordinate", "1"],
  6109. ["Width", "2"],
  6110. ["Height", "3"],
  6111. ]), "dropdown");
  6112. this.setOutput(true, null);
  6113. this.setColour("#f2a247");
  6114. this.setTooltip("");
  6115. this.setHelpUrl("");
  6116. }
  6117. };
  6118. Blockly.Python.ai_vision_dgd = function(block) {
  6119. var n = block.getFieldValue('dropdown');
  6120. var _code = "fc_new_roi[" + n + "]\n"
  6121. return _code;
  6122. }
  6123. Blockly.Blocks['ai_vision_sif'] = {
  6124. init: function() {
  6125. this.appendDummyInput()
  6126. .appendField(Blockly.Msg.VISION_SIF);
  6127. this.appendDummyInput()
  6128. .appendField(Blockly.Msg.VISION_SIF_D)
  6129. .appendField(new Blockly.FieldDropdown([
  6130. ["Black", "DETECT_SINGLE_BLACK_LINE"],
  6131. ["White", "DETECT_SINGLE_WHITE_LINE"]
  6132. ]), "color")
  6133. this.appendStatementInput('DO0')
  6134. .appendField(Blockly.Msg.TOUCH_DO);
  6135. this.setPreviousStatement(true);
  6136. this.setNextStatement(true);
  6137. this.setColour("#f2a247");
  6138. }
  6139. };
  6140. Blockly.Python.ai_vision_sif = function(block) {
  6141. var color = block.getFieldValue('color');
  6142. Blockly.Python.addVariable('ROI', "ROI = (0, 92, 224, 40)", true);
  6143. Blockly.Python.addVariable('DETECT_SINGLE_BLACK_LINE', "[(0, 40)]", true);
  6144. Blockly.Python.addVariable('DETECT_SINGLE_WHITE_LINE', "[(128, 255)]", true);
  6145. Blockly.Python.addVariable('GRAYSCALE_THRESHOLD', "ROI = " + color, true);
  6146. var _code = "def get_blob_value(blob):\n";
  6147. _code += " return blob.w()\n";
  6148. Blockly.Python.addFunction("get_blob_value", _code);
  6149. _code = "def compare_blob(blob1, blob2):\n"
  6150. _code += " comp_result = get_blob_value(blob1) - get_blob_value(blob2)\n"
  6151. _code += " if comp_result > 3:\n"
  6152. _code += " return 1\n"
  6153. _code += " elif comp_result < -3:\n"
  6154. _code += " return -1\n"
  6155. _code += " else:\n"
  6156. _code += " return 0\n"
  6157. Blockly.Python.addFunction("compare_blob", _code);
  6158. _code = "def get_direction(left_blob, right_blob):\n"
  6159. _code += " MAX_WIDTH = 224\n"
  6160. _code += " theta = 0.01\n"
  6161. _code += " b = 3\n"
  6162. _code += " x1 = left_blob.x() - int(0.5 * left_blob.w())\n"
  6163. _code += " x2 = right_blob.x() + int(0.5 * right_blob.w())\n"
  6164. _code += " w_left = x1\n"
  6165. _code += " w_center = math.fabs(x2 - x1)\n"
  6166. _code += " w_right = math.fabs(MAX_WIDTH - x2)\n"
  6167. _code += " direct_ratio = (w_left + b + theta * w_center) / (w_left + w_right + 2 * b + 2 * theta * w_center) - 0.5\n"
  6168. _code += " return direct_ratio\n"
  6169. Blockly.Python.addFunction("get_direction", _code);
  6170. _code = "def get_top2_blobs(blobs):\n"
  6171. _code += " for blob in blobs:\n"
  6172. _code += " pass\n"
  6173. _code += " #print(blob)\n"
  6174. _code += " # img.draw_rectangle(blob.rect())\n"
  6175. _code += " if len(blobs) < 2:\n"
  6176. _code += " return (None, None)\n"
  6177. _code += " top_blob1 = blobs[0]\n"
  6178. _code += " top_blob2 = blobs[1]\n"
  6179. _code += " if compare_blob(top_blob1, top_blob2) == -1:\n"
  6180. _code += " top_blob1, top_blob2 = top_blob2, top_blob1\n"
  6181. _code += " for i in range(2, len(blobs)):\n"
  6182. _code += " if compare_blob(blobs[i], top_blob1) == 1:\n"
  6183. _code += " top_blob2 = top_blob1\n"
  6184. _code += " top_blob1 = blobs[i]\n"
  6185. _code += " elif compare_blob(blobs[i], top_blob2) == 1:\n"
  6186. _code += " top_blob2 = blobs[i]\n"
  6187. _code += " if top_blob1.cx() > top_blob2.cx():\n"
  6188. _code += " return (top_blob2, top_blob1)\n"
  6189. _code += " else:\n"
  6190. _code += " return (top_blob1, top_blob2)\n"
  6191. Blockly.Python.addFunction("get_top2_blobs", _code);
  6192. _code = "def draw_direct(img, direct_ratio):\n"
  6193. _code += " img.draw_circle(112, 112, 5)\n"
  6194. _code += " img.draw_line((112, 112, int(112 + direct_ratio * 20), 112))\n"
  6195. Blockly.Python.addFunction("draw_direct", _code);
  6196. // # b 積木主體代碼
  6197. _code = "img = sensor.snapshot()";
  6198. _code += "blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi = ROI, merge = True)";
  6199. _code += "if blobs:\n"
  6200. _code += " left_blob, right_blob = get_top2_blobs(blobs)\n"
  6201. _code += " if(left_blob == None or right_blob == None):\n"
  6202. _code += " print(\"Out Of Range\")\n"
  6203. _code += " lcd.display(img, oft=(8,8))\n"
  6204. _code += " continue\n"
  6205. _code += " else:\n"
  6206. _code += " print(\"left blob\")\n"
  6207. _code += " print(left_blob)\n"
  6208. _code += " print(\"right blob\")\n"
  6209. _code += " print(right_blob)\n"
  6210. _code += " img.draw_circle(left_blob.cx(), left_blob.cy(), 3, thickness=2)\n"
  6211. _code += " img.draw_circle(right_blob.cx(), right_blob.cy(), 3, thickness=2)\n"
  6212. _code += " img.draw_line(left_blob.cx(), left_blob.cy(), right_blob.cx(), right_blob.cy(), thickness=2)\n"
  6213. _code += " direct_ratio = get_direction(left_blob, right_blob)\n"
  6214. _code += " img.draw_string(10, 33, \"%.2f\"%direct_ratio, mono_space=False)\n"
  6215. _code += " rect_color = (0,0,255)\n"
  6216. _code += " arrow_color = (255,255,255)\n"
  6217. _code += " if float(direct_ratio) < -0.1:\n"
  6218. _code += " img.draw_string(10, 10, \"Turn Left\", scale=2, mono_space=False)\n"
  6219. _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
  6220. _code += " img.draw_arrow(207, 30,180, 30, color=arrow_color, thickness=3)\n"
  6221. _code += " elif float(direct_ratio) > 0.1:\n"
  6222. _code += " img.draw_string(10, 10, \"Turn Right\", scale=2, mono_space=False)\n"
  6223. _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
  6224. _code += " img.draw_arrow(180, 30, 207, 30, color=arrow_color, thickness=3)\n"
  6225. _code += " elif int(direct_ratio) == 0:\n"
  6226. _code += " img.draw_string(10, 10, \"Go Straight\", scale=2, mono_space=False)\n"
  6227. _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
  6228. _code += " img.draw_arrow(194, 40, 194, 20, color=arrow_color, thickness=3)\n"
  6229. _code += " # 將條件積木中放置的代碼放在此處...\n"
  6230. return _code;
  6231. }
  6232. Blockly.Blocks['ai_vision_sgd'] = {
  6233. init: function() {
  6234. this.appendDummyInput()
  6235. .appendField(Blockly.Msg.VISION_SIF_G);
  6236. this.appendDummyInput()
  6237. .appendField(Blockly.Msg.VISION_SIF_GF);
  6238. this.setOutput(true, null);
  6239. this.setColour("#f2a247");
  6240. this.setTooltip("");
  6241. this.setHelpUrl("");
  6242. }
  6243. };
  6244. Blockly.Python.ai_vision_sgd = function(block) {
  6245. var _code = "direct_ratio\n"
  6246. return _code;
  6247. }
  6248. */
  6249. /*
  6250. _ ___ ____ _
  6251. / \ |_ _| / ___| _ _ ___| |_ ___ _ __ ___
  6252. / _ \ | | \___ \| | | / __| __/ _ \ '_ ` _ \
  6253. / ___ \ | | ___) | |_| \__ \ || __/ | | | | |
  6254. /_/ \_\___| |____/ \__, |___/\__\___|_| |_| |_|
  6255. |___/
  6256. */
  6257. Blockly.Blocks['ai_system_restart'] = {
  6258. init: function () {
  6259. this.appendDummyInput()
  6260. .appendField(Blockly.Msg.ai_system_restart);
  6261. this.setPreviousStatement(true);
  6262. this.setNextStatement(true);
  6263. this.setColour("#d42b03");
  6264. this.setTooltip(Blockly.Msg.ai_system_restart_TOOLTIP);
  6265. this.setHelpUrl("");
  6266. }
  6267. };
  6268. Blockly.Python.ai_system_restart = function (block) {
  6269. Blockly.Python.definitions_.import_machine = "import machine";
  6270. var _code = "machine.reset()\n"
  6271. return _code;
  6272. }
  6273. // Blockly.Blocks['ai_system_gc'] = {
  6274. // init: function() {
  6275. // this.appendDummyInput()
  6276. // .appendField(Blockly.Msg.ai_system_enable_gc);
  6277. // this.setInputsInline(false);
  6278. // this.setPreviousStatement(true, null);
  6279. // this.setNextStatement(true, null);
  6280. // this.setColour("#d42b03");
  6281. // this.setTooltip("");
  6282. // this.setHelpUrl("");
  6283. // }
  6284. // };
  6285. // Blockly.Python.ai_system_gc = function(block) {
  6286. // Blockly.Python.definitions_.import_gc = "import gc";
  6287. // Blockly.Python.addSetup("ai_system_gc", "gc.enable()");
  6288. // var _code = "gc.collect()\n"
  6289. // return _code;
  6290. // }
  6291. Blockly.Blocks['ai_system_run'] = {
  6292. init: function () {
  6293. this.appendDummyInput()
  6294. .appendField(Blockly.Msg.ai_system_run_python_code);
  6295. this.appendValueInput("PATH")
  6296. .setCheck(null)
  6297. .appendField(Blockly.Msg.ai_system_run_python_code_file_path);
  6298. this.setInputsInline(false);
  6299. this.setPreviousStatement(true, null);
  6300. this.setNextStatement(true, null);
  6301. this.setColour("#d42b03");
  6302. this.setTooltip(Blockly.Msg.ai_system_run_TOOLTIP);
  6303. this.setHelpUrl("");
  6304. }
  6305. };
  6306. Blockly.Python.ai_system_run = function (block) {
  6307. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  6308. var _code = "exec(open(" + _path + ").read())\n"
  6309. return _code;
  6310. }
  6311. Blockly.Blocks['ai_system_create'] = {
  6312. init: function () {
  6313. this.appendDummyInput()
  6314. .appendField(Blockly.Msg.SYSTEM_CREATE);
  6315. this.appendValueInput("PATH")
  6316. .setCheck(null)
  6317. .appendField(Blockly.Msg.SYSTEM_CREATE_F);
  6318. this.setInputsInline(false);
  6319. this.setPreviousStatement(true, null);
  6320. this.setNextStatement(true, null);
  6321. this.setColour("#1e3283");
  6322. this.setTooltip("");
  6323. this.setHelpUrl("");
  6324. }
  6325. };
  6326. Blockly.Python.ai_system_create = function (block) {
  6327. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  6328. var _code = "def _CREATE_TEXT_FILE(_path):\n"
  6329. _code += " f = open(_path, 'w')\n"
  6330. _code += " f.close()\n"
  6331. Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
  6332. _code = "_CREATE_TEXT_FILE(" + _path + ")\n";
  6333. return _code;
  6334. }
  6335. Blockly.Blocks['ai_system_write'] = {
  6336. init: function () {
  6337. this.appendDummyInput()
  6338. .appendField(Blockly.Msg.SYSTEM_WRITE);
  6339. this.appendValueInput("PATH")
  6340. .setCheck(null)
  6341. .appendField(Blockly.Msg.SYSTEM_WRITE_F);
  6342. this.appendValueInput("WPATH")
  6343. .setCheck(null)
  6344. .appendField(Blockly.Msg.SYSTEM_WRITE_W);
  6345. this.appendDummyInput()
  6346. .appendField(Blockly.Msg.SYSTEM_WRITE_S)
  6347. .appendField(new Blockly.FieldDropdown([
  6348. [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
  6349. [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
  6350. [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
  6351. [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
  6352. ]), "separator");
  6353. this.setInputsInline(false);
  6354. this.setPreviousStatement(true, null);
  6355. this.setNextStatement(true, null);
  6356. this.setColour("#1e3283");
  6357. this.setTooltip("");
  6358. this.setHelpUrl("");
  6359. }
  6360. };
  6361. Blockly.Python.ai_system_write = function (block) {
  6362. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  6363. var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
  6364. var _separator = block.getFieldValue('separator');
  6365. var _code = "def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):\n"
  6366. _code += " f = open(_path, 'a')\n"
  6367. _code += " f.write(_data + _sep)\n"
  6368. _code += " f.close()\n"
  6369. Blockly.Python.addFunction("_CREATE_TEXT_FILE_WITH_CONTENT", _code);
  6370. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
  6371. if (_separator == "newline") {
  6372. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
  6373. } else if (_separator == "comma") {
  6374. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ',')\n";
  6375. } else if (_separator == "space") {
  6376. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ' ')\n";
  6377. } else if (_separator == "semicolon") {
  6378. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ';')\n";
  6379. }
  6380. return _code;
  6381. }
  6382. Blockly.Blocks['ai_system_input'] = {
  6383. init: function () {
  6384. this.appendDummyInput()
  6385. .appendField(Blockly.Msg.SYSTEM_INPUT);
  6386. this.appendValueInput("PATH")
  6387. .setCheck(null)
  6388. .appendField(Blockly.Msg.SYSTEM_WRITE_F);
  6389. this.appendDummyInput()
  6390. .appendField(Blockly.Msg.SYSTEM_WRITE_S)
  6391. .appendField(new Blockly.FieldDropdown([
  6392. [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
  6393. [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
  6394. [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
  6395. [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
  6396. ]), "separator");
  6397. this.setInputsInline(false);
  6398. this.setPreviousStatement(true, null);
  6399. this.setNextStatement(true, null);
  6400. this.setColour("#1e3283");
  6401. this.setTooltip("");
  6402. this.setHelpUrl("");
  6403. }
  6404. };
  6405. Blockly.Python.ai_system_input = function (block) {
  6406. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  6407. var _separator = block.getFieldValue('separator');
  6408. var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n"
  6409. _code += " f = open(_path, 'r')\n"
  6410. _code += " result = f.read().split(_sep)\n"
  6411. _code += " f.close()\n"
  6412. _code += " return result\n"
  6413. Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
  6414. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  6415. if (_separator == "newline") {
  6416. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  6417. } else if (_separator == "comma") {
  6418. _code = "_GET_LIST_FROM_FILE(" + _path + ", ',')";
  6419. } else if (_separator == "space") {
  6420. _code = "_GET_LIST_FROM_FILE(" + _path + ", ' ')";
  6421. } else if (_separator == "semicolon") {
  6422. _code = "_GET_LIST_FROM_FILE(" + _path + ", ';')";
  6423. }
  6424. return _code;
  6425. }
  6426. /*
  6427. __ ___ _____ _
  6428. \ \ / (_) ___(_)
  6429. \ \ /\ / /| | |_ | |
  6430. \ V V / | | _| | |
  6431. \_/\_/ |_|_| |_|
  6432. */
  6433. var K210_WIFI_COLOR = "#3062c1";
  6434. Blockly.Blocks['k210_wifi_enable_disable'] = {
  6435. init: function () {
  6436. this.appendDummyInput()
  6437. .appendField(Blockly.Msg.ai_wifi_set_wifi)
  6438. .appendField(new Blockly.FieldDropdown([
  6439. [Blockly.Msg.ai_wifi_enabled, "1"],
  6440. [Blockly.Msg.ai_wifi_disabled, "0"]
  6441. ]), "de");
  6442. this.setPreviousStatement(true, null);
  6443. this.setNextStatement(true, null);
  6444. this.setColour(K210_WIFI_COLOR);
  6445. var thisBlock = this;
  6446. this.setTooltip(function () {
  6447. var mode = thisBlock.getFieldValue('de');
  6448. var TOOLTIPS = {
  6449. '1': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_enabled),
  6450. '0': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_disabled)
  6451. };
  6452. return TOOLTIPS[mode];
  6453. });
  6454. this.setHelpUrl("");
  6455. }
  6456. };
  6457. Blockly.Python['k210_wifi_enable_disable'] = function (block) {
  6458. var dropdown_de = block.getFieldValue('de');
  6459. // Blockly.Python.addVariable('k210_wifi_enable_disable', '', true);
  6460. Blockly.Python.definitions_['k210_wifi_enable_disable_def'] = '' +
  6461. 'from machine import UART\n' +
  6462. 'import network, time\n' +
  6463. 'from machine import UART\n' +
  6464. 'from Maix import GPIO\n' +
  6465. 'from fpioa_manager import fm, board_info\n' +
  6466. '\n' +
  6467. '# En SEP8285\n' +
  6468. 'fm.register(8, fm.fpioa.GPIOHS0, force=True)\n' +
  6469. '_k210_wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)\n' +
  6470. '\n' +
  6471. 'fm.register(0, fm.fpioa.GPIOHS1, force=True)\n' +
  6472. '_k210wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)\n' +
  6473. '_k210wifi_io0_en.value(0)\n' +
  6474. '\n' +
  6475. 'fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)\n' +
  6476. 'fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)\n' +
  6477. '\n' +
  6478. '_k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
  6479. '\n' +
  6480. 'def _k210_wifi_enable(en):\n' +
  6481. ' global _k210_wifi_en\n' +
  6482. ' _k210_wifi_en.value(en)\n' +
  6483. '';
  6484. // TODO: Assemble Python into code variable.
  6485. if (dropdown_de == "1") {
  6486. var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
  6487. 'time.sleep(2)\n' +
  6488. '_k210_wifi = network.ESP8285(_k210_uart)\n' +
  6489. '\n';
  6490. } else if (dropdown_de == "0") {
  6491. var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
  6492. '\n';
  6493. }
  6494. return code;
  6495. };
  6496. Blockly.Blocks['k210_wifi_scan'] = {
  6497. init: function () {
  6498. this.appendDummyInput()
  6499. .appendField(new Blockly.FieldImage("blockly/media/wifi_scan.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  6500. this.appendDummyInput()
  6501. .appendField(Blockly.Msg.ai_wifi_start_wifi_scan);
  6502. this.setPreviousStatement(true, null);
  6503. this.setNextStatement(true, null);
  6504. this.setColour(K210_WIFI_COLOR);
  6505. this.setTooltip(Blockly.Msg.k210_wifi_scan_TOOLTIP);
  6506. this.setHelpUrl("");
  6507. }
  6508. };
  6509. Blockly.Python['k210_wifi_scan'] = function (block) {
  6510. // TODO: Assemble Python into code variable.
  6511. Blockly.Python.definitions_['k210_wifi_scan_def'] = '' +
  6512. 'def wifi_deal_ap_info(info):\n' +
  6513. ' res = []\n' +
  6514. ' for ap_str in info:\n' +
  6515. ' ap_str = ap_str.split(",")\n' +
  6516. ' info_one = []\n' +
  6517. ' for node in ap_str:\n' +
  6518. ' if node.startswith(\'\"\'):\n' +
  6519. ' info_one.append(node[1:-1])\n' +
  6520. ' else:\n' +
  6521. ' info_one.append(int(node))\n' +
  6522. ' res.append(info_one)\n' +
  6523. ' return res\n' +
  6524. '';
  6525. var code = '' +
  6526. '_k210_ap_scan_info = _k210_wifi.scan()\n' +
  6527. '_k210_ap_scan_info = wifi_deal_ap_info(_k210_ap_scan_info)\n' +
  6528. '_k210_ap_scan_info.sort(key=lambda x:x[2], reverse=True) # sort by rssi\n' +
  6529. '\n';
  6530. return code;
  6531. };
  6532. Blockly.Blocks['k210_wifi_scan_get'] = {
  6533. init: function () {
  6534. this.appendDummyInput()
  6535. .appendField(Blockly.Msg.ai_wifi_wifi_scan_result);
  6536. this.setOutput(true, null);
  6537. this.setColour(K210_WIFI_COLOR);
  6538. this.setTooltip(Blockly.Msg.k210_wifi_scan_get_TOOLTIP);
  6539. this.setHelpUrl("");
  6540. }
  6541. };
  6542. Blockly.Python['k210_wifi_scan_get'] = function (block) {
  6543. // TODO: Assemble Python into code variable.
  6544. var code = '_k210_ap_scan_info';
  6545. // TODO: Change ORDER_NONE to the correct strength.
  6546. return [code, Blockly.Python.ORDER_NONE];
  6547. };
  6548. Blockly.Blocks['k210_wifi_connect_hotspot'] = {
  6549. init: function () {
  6550. this.appendDummyInput()
  6551. .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  6552. this.appendDummyInput()
  6553. .appendField(Blockly.Msg.ai_wifi_connect_wifi);
  6554. this.appendValueInput("ssid")
  6555. .setCheck(null)
  6556. .appendField(Blockly.Msg.ai_wifi_connect_wifi_ssid);
  6557. this.appendValueInput("pass")
  6558. .setCheck(null)
  6559. .appendField(Blockly.Msg.ai_wifi_connect_wifi_pass);
  6560. this.setPreviousStatement(true, null);
  6561. this.setNextStatement(true, null);
  6562. this.setColour(K210_WIFI_COLOR);
  6563. this.setTooltip(Blockly.Msg.k210_wifi_connect_hotspot_TOOLTIP);
  6564. this.setHelpUrl("");
  6565. }
  6566. };
  6567. Blockly.Python['k210_wifi_connect_hotspot'] = function (block) {
  6568. var value_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
  6569. var value_pass = Blockly.Python.valueToCode(block, 'pass', Blockly.Python.ORDER_ATOMIC);
  6570. // TODO: Assemble Python into code variable.
  6571. Blockly.Python.definitions_['k210_wifi_connect_hotspot_def'] = '' +
  6572. 'def _k210_wifi_reset():\n' +
  6573. ' global _k210_uart\n' +
  6574. ' _k210_wifi_enable(0)\n' +
  6575. ' time.sleep_ms(200)\n' +
  6576. ' _k210_wifi_enable(1)\n' +
  6577. ' time.sleep(2)\n' +
  6578. ' _k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
  6579. ' tmp = _k210_uart.read()\n' +
  6580. ' _k210_uart.write("AT+UART_CUR=921600,8,1,0,0\\r\\n")\n' +
  6581. ' print(_k210_uart.read())\n' +
  6582. ' _k210_uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240)\n' +
  6583. ' _k210_uart.write("AT\\r\\n")\n' +
  6584. ' tmp = _k210_uart.read()\n' +
  6585. ' print(tmp)\n' +
  6586. ' if not tmp.endswith("OK\\r\\n"):\n' +
  6587. ' print("reset fail")\n' +
  6588. ' return None\n' +
  6589. ' try:\n' +
  6590. ' _k210_wifi = network.ESP8285(_k210_uart)\n' +
  6591. ' except Exception:\n' +
  6592. ' return None\n' +
  6593. ' return _k210_wifi\n' +
  6594. '';
  6595. var code = '' +
  6596. '_k210_wifi = _k210_wifi_reset()\n' +
  6597. 'if not _k210_wifi:\n' +
  6598. ' raise Exception("WiFi init fail")\n' +
  6599. '\n' +
  6600. '_k210_wifi.connect(' + value_ssid + ',' + value_pass + ')\n' +
  6601. '\n';
  6602. return code;
  6603. };
  6604. Blockly.Blocks['k210_wifi_get_connection_status'] = {
  6605. init: function () {
  6606. this.appendDummyInput()
  6607. .appendField(Blockly.Msg.ai_wifi_get_connection_status);
  6608. this.setOutput(true, null);
  6609. this.setColour(K210_WIFI_COLOR);
  6610. this.setTooltip(Blockly.Msg.k210_wifi_get_connection_status_TOOLTIP);
  6611. this.setHelpUrl("");
  6612. }
  6613. };
  6614. Blockly.Python['k210_wifi_get_connection_status'] = function (block) {
  6615. // TODO: Assemble Python into code variable.
  6616. var code = '_k210_wifi.isconnected()';
  6617. // TODO: Change ORDER_NONE to the correct strength.
  6618. return [code, Blockly.Python.ORDER_NONE];
  6619. };
  6620. Blockly.Blocks['k210_wifi_get_config'] = {
  6621. init: function () {
  6622. this.appendDummyInput()
  6623. .appendField(Blockly.Msg.ai_wifi_get_wifi_info)
  6624. .appendField(new Blockly.FieldDropdown([
  6625. [Blockly.Msg.ai_wifi_get_wifi_info_name, "6"],
  6626. [Blockly.Msg.ai_wifi_get_wifi_info_ip, "0"],
  6627. [Blockly.Msg.ai_wifi_get_wifi_info_dns, "2"],
  6628. [Blockly.Msg.ai_wifi_get_wifi_info_mac, "5"]
  6629. ]), "type");
  6630. this.setOutput(true, null);
  6631. this.setColour(K210_WIFI_COLOR);
  6632. this.setTooltip(Blockly.Msg.k210_wifi_get_config_TOOLTIP);
  6633. this.setHelpUrl("");
  6634. }
  6635. };
  6636. Blockly.Python['k210_wifi_get_config'] = function (block) {
  6637. var dropdown_type = block.getFieldValue('type');
  6638. // TODO: Assemble Python into code variable.
  6639. var code = '_k210_wifi.ifconfig()[' + dropdown_type + ']';
  6640. // TODO: Change ORDER_NONE to the correct strength.
  6641. return [code, Blockly.Python.ORDER_NONE];
  6642. };
  6643. Blockly.Blocks['k210_wifi_cococloud'] = {
  6644. init: function () {
  6645. this.appendDummyInput()
  6646. .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
  6647. this.appendDummyInput()
  6648. .appendField(Blockly.Msg.iotservice_cococloud_send_title);
  6649. this.appendDummyInput()
  6650. .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
  6651. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  6652. /*this.appendValueInput("first_input")
  6653. .setCheck(null)
  6654. .appendField("Field 0");*/
  6655. this.itemCount_ = 1;
  6656. this.updateShape_();
  6657. this.setMutator(new Blockly.Mutator(['k210_wifi_cococloud_create_with_item']));
  6658. this.setColour(K210_WIFI_COLOR);
  6659. this.setPreviousStatement(true, null);
  6660. this.setNextStatement(true, null);
  6661. this.setTooltip("");
  6662. this.setHelpUrl("");
  6663. },
  6664. mutationToDom: function () {
  6665. var container = document.createElement('mutation');
  6666. container.setAttribute('items', this.itemCount_);
  6667. return container;
  6668. },
  6669. domToMutation: function (xmlElement) {
  6670. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  6671. this.updateShape_();
  6672. },
  6673. decompose: function (workspace) {
  6674. var containerBlock = workspace.newBlock('k210_wifi_cococloud_create_with_container');
  6675. containerBlock.initSvg();
  6676. var connection = containerBlock.getInput('STACK').connection;
  6677. for (var i = 0; i < this.itemCount_; i++) {
  6678. var itemBlock = workspace.newBlock('k210_wifi_cococloud_create_with_item');
  6679. itemBlock.initSvg();
  6680. connection.connect(itemBlock.previousConnection);
  6681. connection = itemBlock.nextConnection;
  6682. }
  6683. return containerBlock;
  6684. },
  6685. compose: function (containerBlock) {
  6686. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  6687. // Count number of inputs.
  6688. var connections = [];
  6689. while (itemBlock) {
  6690. connections.push(itemBlock.valueConnection_);
  6691. itemBlock = itemBlock.nextConnection &&
  6692. itemBlock.nextConnection.targetBlock();
  6693. }
  6694. // Disconnect any children that don't belong.
  6695. for (var i = 0; i < this.itemCount_; i++) {
  6696. var connection = this.getInput('ADD' + i).connection.targetConnection;
  6697. if (connection && connections.indexOf(connection) == -1) {
  6698. connection.disconnect();
  6699. }
  6700. }
  6701. this.itemCount_ = connections.length;
  6702. this.updateShape_();
  6703. // Reconnect any child blocks.
  6704. for (var i = 0; i < this.itemCount_; i++) {
  6705. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  6706. }
  6707. },
  6708. saveConnections: function (containerBlock) {
  6709. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  6710. var i = 0;
  6711. while (itemBlock) {
  6712. var input = this.getInput('ADD' + i);
  6713. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  6714. i++;
  6715. itemBlock = itemBlock.nextConnection &&
  6716. itemBlock.nextConnection.targetBlock();
  6717. }
  6718. },
  6719. updateShape_: function () {
  6720. for (var i = 0; i < this.itemCount_; i++) {
  6721. if (!this.getInput('ADD' + i)) {
  6722. var input = this.appendValueInput('ADD' + i);
  6723. input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
  6724. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  6725. //input.appendField("資料 " + (i + 1) + ":");
  6726. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  6727. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  6728. }
  6729. }
  6730. while (this.getInput('ADD' + i)) {
  6731. this.removeInput('ADD' + i);
  6732. i++;
  6733. }
  6734. },
  6735. };
  6736. Blockly.Blocks['k210_wifi_cococloud_create_with_container'] = {
  6737. init: function () {
  6738. this.setColour(K210_WIFI_COLOR);
  6739. this.appendDummyInput()
  6740. .appendField("Items");
  6741. this.appendStatementInput('STACK');
  6742. this.setTooltip('');
  6743. this.contextMenu = false;
  6744. }
  6745. };
  6746. Blockly.Blocks['k210_wifi_cococloud_create_with_item'] = {
  6747. init: function () {
  6748. this.setColour(K210_WIFI_COLOR);
  6749. this.appendDummyInput()
  6750. .appendField("Field");
  6751. this.setPreviousStatement(true);
  6752. this.setNextStatement(true);
  6753. this.setTooltip('');
  6754. this.contextMenu = false;
  6755. }
  6756. };
  6757. Blockly.Python['k210_wifi_cococloud'] = function (block) {
  6758. var api = block.getFieldValue('t_api');
  6759. Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '', true);
  6760. Blockly.Python.definitions_['import_urequests'] = '' +
  6761. 'import urequests\n' +
  6762. 'import ujson as json\n'
  6763. '';
  6764. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  6765. var item_field = '',
  6766. item_value = '';
  6767. var cococloud_data = "'{";
  6768. for (var n = 0; n < this.itemCount_; n++) {
  6769. item_field = block.getFieldValue("field" + n);
  6770. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  6771. cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  6772. }
  6773. var cococloud_data_final = cococloud_data + "}'";
  6774. var code = '' +
  6775. '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
  6776. '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  6777. 'try:\n' +
  6778. ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n' +
  6779. 'except BaseException as e:\n' +
  6780. ' print(e)\n' +
  6781. 'pass\n' +
  6782. '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n'
  6783. '';
  6784. // cococloud_data_final.slice(0, -3) + '}\''
  6785. return code;
  6786. };
  6787. Blockly.Blocks['k210_wifi_cococloud_read'] = {
  6788. init: function () {
  6789. this.appendDummyInput()
  6790. .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
  6791. this.appendDummyInput()
  6792. .appendField(Blockly.Msg.iotservice_cococloud_get_title);
  6793. this.appendDummyInput()
  6794. .appendField(Blockly.Msg.iotservice_cococloud_get_api)
  6795. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "api");
  6796. this.setInputsInline(false);
  6797. this.setPreviousStatement(true, null);
  6798. this.setNextStatement(true, null);
  6799. this.setColour(K210_WIFI_COLOR);
  6800. this.setTooltip("");
  6801. this.setHelpUrl("");
  6802. }
  6803. };
  6804. Blockly.Python['k210_wifi_cococloud_read'] = function (block) {
  6805. var api = block.getFieldValue('api');
  6806. Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '', true);
  6807. Blockly.Python.definitions_['import_urequests'] = '' +
  6808. 'import urequests\n' +
  6809. 'import ujson as json\n'
  6810. '';
  6811. // TODO: Assemble Python into code variable.
  6812. var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
  6813. return code;
  6814. };
  6815. Blockly.Blocks['k210_wifi_cococloud_read_data'] = {
  6816. init: function () {
  6817. this.appendDummyInput()
  6818. .appendField(Blockly.Msg.iotservice_cococloud_get_property);
  6819. this.appendValueInput("property")
  6820. .setCheck(null);
  6821. this.appendDummyInput()
  6822. .appendField(Blockly.Msg.iotservice_cococloud_get_data);
  6823. this.setInputsInline(true);
  6824. this.setOutput(true, null);
  6825. this.setColour(K210_WIFI_COLOR);
  6826. this.setTooltip("");
  6827. this.setHelpUrl("");
  6828. }
  6829. };
  6830. Blockly.Python['k210_wifi_cococloud_read_data'] = function (block) {
  6831. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  6832. // TODO: Assemble Python into code variable.
  6833. var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
  6834. // TODO: Change ORDER_NONE to the correct strength.
  6835. return [code, Blockly.Python.ORDER_NONE];
  6836. };