123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533 |
- /*
- _ ___ ____ _
- / \ |_ _| | __ ) __ _ ___(_) ___
- / _ \ | | | _ \ / _` / __| |/ __|
- / ___ \ | | | |_) | (_| \__ \ | (__
- /_/ \_\___| |____/ \__,_|___/_|\___|
- */
- Blockly.Blocks['ai_k210_button_read_pressed'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_button_when)
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type")
- .appendField(Blockly.Msg.ai_basic_pressed)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'a': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
- 'c': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_button_read_pressed'] = function (block) {
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- var _type = block.getFieldValue('button_type');
- var code = '_gp_side_' + _type + '.value() == 1';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['ai_k210_button_read_released'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_button_when)
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type_released")
- .appendField(Blockly.Msg.ai_basic_released)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('button_type_released');
- var TOOLTIPS = {
- 'a': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "B"),
- 'c': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "C")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_button_read_released'] = function (block) {
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- var _type = block.getFieldValue('button_type_released');
- var code = '_gp_side_' + _type + '.value() == 0';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['ai_k210_led_light_up'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_blue, "blue"],
- [Blockly.Msg.ai_basic_led_red, "red"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_on);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'blue': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
- 'red': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_led_light_up'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['k210_led_light_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- '\n' +
- 'fm.register(31, fm.fpioa.GPIO3)\n' +
- 'fm.register(32, fm.fpioa.GPIO4)\n' +
- '\n' +
- '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
- '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_led_' + dropdown_type + '.value(1)\n';
- return code;
- };
- Blockly.Blocks['ai_k210_led_light_off'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_blue, "blue"],
- [Blockly.Msg.ai_basic_led_red, "red"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_off);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'blue': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
- 'red': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_led_light_off'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['k210_led_light_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- '\n' +
- 'fm.register(31, fm.fpioa.GPIO3)\n' +
- 'fm.register(32, fm.fpioa.GPIO4)\n' +
- '\n' +
- '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
- '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_led_' + dropdown_type + '.value(0)\n';
- return code;
- };
- Blockly.Blocks['ai_basic_light_up_led_analog'] = {
- init: function () {
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_basic_led_analog_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_analog_led1, "1"],
- [Blockly.Msg.ai_basic_led_analog_led2, "2"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_analog_brightness_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_analog_range_text);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '1': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led1),
- '2': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led2)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_basic_light_up_led_analog'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['ai_basic_light_up_led_analog_def'] = '' +
- 'from machine import Timer,PWM\n' +
- 'import time\n' +
- '\n' +
- 'led_left_tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM)\n' +
- 'led_right_tim = Timer(Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM)\n' +
- 'led_left_ctrl = PWM(led_left_tim, freq=500000, duty=50, pin=31)\n' +
- 'led_right_ctrl = PWM(led_right_tim, freq=500000, duty=50, pin=32)\n' +
- '\n' +
- 'def analog_light(which, brightness):\n' +
- ' if which == 1:\n' +
- ' led_left_ctrl.duty(brightness)\n' +
- ' if which == 2:\n' +
- ' led_right_ctrl.duty(brightness)\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = 'analog_light(' + dropdown_type + ',' + value_brightness + ')\n';
- return code;
- };
- /*
- _ ___ ___ ____
- / \ |_ _| |_ _|_ __ ___ __ _ __ _ ___ | _ \ _ __ ___ ___ ___ ___ ___
- / _ \ | | | || '_ ` _ \ / _` |/ _` |/ _ \ | |_) | '__/ _ \ / __/ _ \/ __/ __|
- / ___ \ | | | || | | | | | (_| | (_| | __/ | __/| | | (_) | (_| __/\__ \__ \
- /_/ \_\___| |___|_| |_| |_|\__,_|\__, |\___| |_| |_| \___/ \___\___||___/___/
- |___/
- */
- Blockly.Blocks["ai_lcd_screeninit"] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/screen_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_init);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_screeninit_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_screeninit = function (block) {
- var checkbox_name = block.getFieldValue('DEGREE');
- //var dropdown_name = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
- Blockly.Python.definitions_.import_lcd = "import lcd";
- Blockly.Python.definitions_.import_image = "import image";
- Blockly.Python.definitions_['import_define_x_ai_screeninit'] = '' +
- 'try:from cocorobo import display_cjk_string\n' +
- 'except:pass\n' +
- '\n' +
- 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n' +
- ' try: \n' +
- ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n' +
- ' return canvas\n' +
- ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n' +
- '\n';
- var _code = "lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n";
- _code += "lcd.rotation(1)\n";
- _code += "lcd.clear(lcd.BLACK)\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_rotation'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_lcd_rotation)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_rotation_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_rotation = function (block) {
- var dropdown_name = block.getFieldValue('DEGREE');
- var _code = "lcd.rotation(" + dropdown_name + ")\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_filp'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_lcd_mirror_flip)
- .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_filp_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_filp = function (block) {
- var _tf = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
- var _code = "lcd.mirror(" + _tf + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_screen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_filled_screen_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_set_filled_screen_color_red, "RED"],
- [Blockly.Msg.image_process_set_filled_screen_color_green, "GREEN"],
- [Blockly.Msg.image_process_set_filled_screen_color_blue, "BLUE"],
- [Blockly.Msg.image_process_set_filled_screen_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_set_filled_screen_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_set_filled_screen_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_set_filled_screen_color_yellow, "YELLOW"],
- [Blockly.Msg.image_process_set_filled_screen_color_black, "BLACK"],
- [Blockly.Msg.image_process_set_filled_screen_color_white, "WHITE"]
- ]), "COLOR");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('COLOR');
- var TOOLTIPS = {
- 'RED': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_red),
- 'GREEN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_green),
- 'BLUE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_blue),
- 'PURPLE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_purple),
- 'CYAN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_cyan),
- 'ORANGE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_orange),
- 'YELLOW': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_yellow),
- 'BLACK': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_black),
- 'WHITE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_white)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_screen = function (block) {
- var colour_name = block.getFieldValue('COLOR');
- var _code = "lcd.clear(lcd." + colour_name + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_fill_screen_with_rgb'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_filled_screen_color);
- this.appendValueInput("rgb_value")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_fill_screen_with_rgb_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_fill_screen_with_rgb'] = function (block) {
- var value_name = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'lcd.clear(' + value_name + ')\n';
- return code;
- };
- Blockly.Blocks['ai_lcd_clear'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_clear_lcd);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_clear_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_clear = function (block) {
- var _code = "lcd.clear(lcd.BLACK)\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_rgb_value_input'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_r);
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_g);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_b);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_rgb_value_input_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_rgb_value_input'] = function (block) {
- var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks.ai_lcd_XY = {
- init: function () {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setTooltip(Blockly.Msg.ai_lcd_XY_TOOLTIP);
- this.setColour("#5bb2d6");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.image_process_xy_x);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.image_process_xy_y)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_XY = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks.ai_lcd_WH = {
- init: function () {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setTooltip(Blockly.Msg.ai_lcd_WH_TOOLTIP);
- this.setColour("#5bb2d6");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.image_process_wh_width);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.image_process_wh_height)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_WH = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_lcd_colortext'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
- [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
- [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
- [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
- [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
- [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
- ]), "COLOR");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text_bg_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
- [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
- [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
- [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
- [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
- [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
- ]), "BCOLOR");
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- /* this.appendDummyInput()
- .appendField(Blockly.Msg.LCDDT); */
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['ai_lcd_colortext'] = function (block) {
- var color = block.getFieldValue("COLOR");
- var bcolor = block.getFieldValue("BCOLOR");
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = "lcd.draw_string(" + xy + ", " + content + ", lcd." + color + ", lcd." + bcolor + ")\n"; //, scale=" + size + "
- // return [_code, Blockly.Python.ORDER_ATOMIC]
- return _code;
- };
- Blockly.Blocks["ai_lcd_createnonecarvas"] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_blank_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_size);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.LCDDCC_F)
- // .appendField(new Blockly.FieldCheckbox("TRUE"), "check")
- // .appendField(Blockly.Msg.LCDDCC_I)
- // .appendField(new Blockly.FieldTextInput(""), "text");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_createnonecarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_createnonecarvas = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + " = image.Image(size=(" + value_wh + "))\n";
- return _code;
- }
- Blockly.Blocks["ai_lcd_fillcanvas"] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_let_canvas_filled_with_color);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_fillcanvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_fillcanvas = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + '.draw_rectangle(0,0, ' + variable_name + '.width(),' + variable_name + '.height(), color=' + value_color + ', thickness=1, fill=True)\n';
- return _code;
- }
- Blockly.Blocks["ai_lcd_createcarvas"] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_image_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_image_canvas_path)
- .appendField(new Blockly.FieldTextInput("/sd/preset/images/splash_bg.jpg"), "text");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_createcarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_createcarvas = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var checkbox_name = block.getFieldValue('check') == 'TRUE' ? "True" : "False";
- var text_name = block.getFieldValue('text');
- var _code = variable_name + " = image.Image(\"" + text_name + "\")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_showcarvas_set_cord'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_set_canvas_setup)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord_as);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_cord_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas_set_cord = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = "_" + variable_name + "_x, _" + variable_name + "_y = " + value_name + "\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_showcarvas_set_display'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_show_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_display_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas_set_display = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.definitions_.ai_lcd_display_def = "" +
- "_" + variable_name + "_x, _" + variable_name + "_y = 0, 0" +
- "";
- // TODO: Assemble Python into code variable.
- var code = "lcd.display(" + variable_name + ", oft=(_" + variable_name + "_x,_" + variable_name + "_y))\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_showcarvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_show_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = "lcd.display(" + variable_name + ", oft=(" + value_name + "))\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_clearcanvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_clear_canvas_1)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_clear_canvas_2);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_clearcanvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_clearcanvas = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".clear()\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_textcarvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_draw_text_font_size);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_draw_text_if_monospace)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "bool");
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_textcarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_textcarvas = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=(" + d + "," + e + "," + f + "), scale=" + size + ", mono_space=" + bool + ")\n";
- } else if (color.charAt(0) != '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=" + color + ", scale=" + size + ", mono_space=" + bool + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_characterscarvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_hanzi_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_characters);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_draw_text_font_size + ":")
- // .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SMALL, "1"],
- [Blockly.Msg.MIDDLE, "2"],
- [Blockly.Msg.LARGE, "3"]
- ]), "fonts");
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.Set_Auto_Wrap + ":")
- // // .setCheck(null)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.LED_SCROLL_YES, "True"],
- // [Blockly.Msg.LED_SCROLL_NO, "False"]
- // ]), "wraps");
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_characterscarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_characterscarvas = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = block.getFieldValue('fonts');;
- // var wraps = block.getFieldValue('wraps');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=(" + d + "," + e + "," + f + "))\n";
- } else if (color.charAt(0) != '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = block.getFieldValue('fonts');;
- // var wraps = block.getFieldValue('wraps');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=" + color + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_linecarvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_line);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_end_coord);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_linecarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_linecarvas = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=(" + d + "," + e + "," + f + "), thickness=" + size + ")\n";
- } else if (color.charAt(0) != '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=" + color + ", thickness=" + size + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_ractanglecarvas'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_text_filled, "fillrect"],
- [Blockly.Msg.image_process_text_stroked, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.image_process_draw_rectangle_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_size);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('rect_type');
- var TOOLTIPS = {
- 'fillrect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
- 'rect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_ractanglecarvas = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=(" + d + "," + e + "," + f + "), thickness=" + th + ", fill=" + type + ")\n";
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=" + color + ", thickness=" + th + ", fill=" + type + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_drawcirclecarvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_text_filled, "fillrect"],
- [Blockly.Msg.image_process_text_stroked, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.image_process_draw_circle_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_draw_circle_text_start);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_radius);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale_0")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('rect_type');
- var TOOLTIPS = {
- 'fillrect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
- 'rect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_drawcirclecarvas = function (block) {
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=(" + d + ", " + e + ", " + f + "), thickness=" + th0 + ", fill=" + type + ")\n";
- } else if (color.charAt(0) != '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=" + color + ", thickness=" + th0 + ", fill=" + type + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_draw_cross_sign'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_cross.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_cross_title);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_centerpos);
- this.appendValueInput("size")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_length);
- this.appendValueInput("thickness")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_thick);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_color);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_cross_sign_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_cross_sign'] = function (block) {
- var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_cross(' + value_xy + ', color=(' + d + ', ' + e + ', ' + f + '), size=' + value_size + ', thickness=' + value_thickness + ')\n';
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_cross(' + value_xy + ', color=' + color + ', size=' + value_size + ', thickness=' + value_thickness + ')\n';
- }
- return code;
- };
- Blockly.Blocks['ai_lcd_draw_arrow_sign'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_arrow.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_arrow_title);
- this.appendValueInput("xy1")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_start);
- this.appendValueInput("xy2")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_end);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_color);
- this.appendValueInput("thickness")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_arrow_sign_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_arrow_sign'] = function (block) {
- // TODO: Assemble Python into code variable.
- var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
- var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + d + ', ' + e + ', ' + f + '), thickness=' + value_thickness + ')\n';
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
- var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + color + '), thickness=' + value_thickness + ')\n';
- }
- return code;
- };
- Blockly.Blocks['ai_lcd_draw_image_on_canvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_image.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_image_title);
- this.appendValueInput("image_path")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_title_image_path);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("scale_x")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_scale_x);
- this.appendValueInput("scale_y")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_scale_y);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_image_on_canvas_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_image_on_canvas'] = function (block) {
- // TODO: Assemble Python into code variable.
- // nav_up = image.Image("/sd/preset/images/arrow_up_filled.jpg")
- // splash.draw_image(nav_up, 5, 5)
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_image_path = Blockly.Python.valueToCode(block, 'image_path', Blockly.Python.ORDER_ATOMIC);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_scale_x = Blockly.Python.valueToCode(block, 'scale_x', Blockly.Python.ORDER_ATOMIC);
- var value_scale_y = Blockly.Python.valueToCode(block, 'scale_y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- variable_name + '.draw_image(image.Image(' + value_image_path + '), ' + value_xy + ', x_scale=' + value_scale_x + ', y_scale=' + value_scale_y + ' )' +
- '\n';
- return code;
- };
- Blockly.Blocks['ai_img_save'] = {
- init: function () {
- this.appendValueInput("save_path")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_img_save);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_img_save_quality)
- .appendField(new Blockly.FieldNumber(90, 0, 100), "compression")
- .appendField("%");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_img_save_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_img_save'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
- var compression_rate = block.getFieldValue('compression');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.save(' + save_path_location + ', quality=' + compression_rate + ')\n';
- return code;
- };
- Blockly.Blocks['ai_lcd_invert_color'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_color_invert);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_invert_color'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.invert()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_resizecanvas'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_resize_to);
- this.appendValueInput("wh")
- .setCheck(null);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_resizecanvas_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_resizecanvas'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.resize(' + wh + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_color_scheme'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_color_scheme_convert)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_color_scheme_bw, "to_grayscale(1)"],
- [Blockly.Msg.image_process_color_scheme_rgb, "to_rgb565(1)"]
- ]), "color_scheme");
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('color_scheme');
- var TOOLTIPS = {
- 'to_grayscale(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_bw),
- 'to_rgb565(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_rgb)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_color_scheme'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_vari = block.getFieldValue('color_scheme');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_vari;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_ai_pix'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_pixtovec_text_first)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_pixtovec_v2p, ".ai_to_pix()"],
- [Blockly.Msg.image_process_pixtovec_p2v, ".pix_to_ai()"]
- ]), "ai_pix_type")
- .appendField(Blockly.Msg.image_process_pixtovec_conversion);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#f0983e");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_ai_pix'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_vari = block.getFieldValue('ai_pix_type');
- // TODO: Assemble Python into code variable.
- var code = 'a = ' + variable_name + dropdown_vari + '\n';
- // TODO: Change ORDER_NONE to the correct strength.
- return code;
- };
- Blockly.Blocks['ai_lcd_img_cut'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_imgcut_cutting);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_imgcut_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_imgcut_size);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_img_cut_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_img_cut'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- // TODO: Change ORDER_NONE to the correct strength.
- var code = variable_name + '.cut(' + xy + ',' + wh + ')';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_statistics'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_get_statistics_to_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_get_statistics_get_analysis);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_size);
- this.appendValueInput("bins")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_boxes);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_statistics_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_statistics'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- var bins = Blockly.Python.valueToCode(block, 'bins', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_statistics(bins=' + bins + ',roi=(' + xy + ',' + wh + '))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_get)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_histogram_compute);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_region);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_get_histogram_region_xy);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_get_histogram_region_wh);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_histogram_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_histogram'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_histogram(roi=(' + value_xy + ',' + value_wh + '))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram_percentile'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_details_1)
- .appendField(new Blockly.FieldVariable("hist"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_histogram_details_get);
- this.appendValueInput("perc")
- .setCheck(null);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_histogram_percentile_TOOLTIP);
- this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
- }
- };
- Blockly.Python['ai_vision_get_histogram_percentile'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_perc = Blockly.Python.valueToCode(block, 'perc', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_percentile(' + value_perc + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram_percentile_detail'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_1)
- .appendField(new Blockly.FieldVariable("percentile"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_get_histogram_percentile_3, "value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_4, "l_value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_5, "a_value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_6, "b_value()"]
- ]), "option");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('option');
- var TOOLTIPS = {
- 'value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_3),
- 'l_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_4),
- 'a_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_5),
- 'b_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_6)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
- }
- };
- Blockly.Python['ai_vision_get_histogram_percentile_detail'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_option = block.getFieldValue('option');
- var code = variable_name + '.' + dropdown_option;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_lab_rgb_conversion'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lab_rgb_conversion_exec)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lab_rgb_conversion_torgb, "lab_to_rgb"],
- [Blockly.Msg.image_process_lab_rgb_conversion_tolab, "rgb_to_lab"]
- ]), "rgblab");
- this.appendValueInput("color_tuple")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_lab_rgb_conversion_tuple);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('rgblab');
- var TOOLTIPS = {
- 'lab_to_rgb': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_torgb),
- 'rgb_to_lab': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_tolab)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_lab_rgb_conversion'] = function (block) {
- var type = block.getFieldValue('rgblab');
- var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'image.' + type + '(' + tuple + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_rgb_grayscale_conversion'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_exec)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_rgb_grayscale_conversion_torgb, "grayscale_to_rgb"],
- [Blockly.Msg.image_process_rgb_grayscale_conversion_togray, "rgb_to_grayscale"]
- ]), "rgb_g");
- this.appendValueInput("color_tuple")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_set);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('rgb_g');
- var TOOLTIPS = {
- 'grayscale_to_rgb': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_torgb),
- 'rgb_to_grayscale': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_togray)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_rgb_grayscale_conversion'] = function (block) {
- var type = block.getFieldValue('rgb_g');
- var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('rgb_tuple', "", true);
- // TODO: Assemble Python into code variable.
- var code = 'image.' + type + '(' + tuple + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ ____
- / \ |_ _| / ___|__ _ _ __ ___ ___ _ __ __ _
- / _ \ | | | | / _` | '_ ` _ \ / _ \ '__/ _` |
- / ___ \ | | | |__| (_| | | | | | | __/ | | (_| |
- /_/ \_\___| \____\__,_|_| |_| |_|\___|_| \__,_|
- */
- Blockly.Blocks.ai_camera_wh = {
- init: function () {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#60c1bb");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.W);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.H)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_camera_wh = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_camera_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/camera_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_init);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_set_size)
- .appendField(new Blockly.FieldDropdown([
- ["QVGA (320*240)", "QVGA"],
- ["QQVGA (160*120)", "QQVGA"]
- ]), "DEGREE");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_color_mode)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_camera_color_mode_rgb, "RGB565"],
- [Blockly.Msg.ai_camera_camera_color_mode_bw, "GRAYSCALE"]
- ]), "DEGREE1");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_init_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_init = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var degee1 = block.getFieldValue('DEGREE1');
- var checkbox = block.getFieldValue('checkbox') == 'TRUE';
- var w = block.getFieldValue('w');
- var h = block.getFieldValue('h');
- var degee2 = block.getFieldValue('DEGREE2') || 1;
- var degee3 = block.getFieldValue('DEGREE3') || 1;
- var degee4 = block.getFieldValue('DEGREE4') || 1;
- var degee5 = block.getFieldValue('DEGREE5') || 1;
- var input1 = block.getFieldValue('input1') || 1;
- var input2 = block.getFieldValue('input2') || 1;
- var input3 = block.getFieldValue('input3') || 1;
- Blockly.Python.definitions_.import_sensor = "import sensor";
- var _code = "sensor.reset()\n";
- _code += "sensor.set_pixformat(sensor." + degee1 + ")\n";
- _code += "sensor.set_framesize(sensor." + degee + ")\n";
- _code += "sensor.set_vflip(0)\n";
- _code += "sensor.set_hmirror(0)\n";
- _code += "sensor.skip_frames(30)\n";
- _code += "sensor.run(1)\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_windows'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_windowing_resize);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_windowing_w)
- .appendField(new Blockly.FieldNumber(224), "window_w")
- .appendField(Blockly.Msg.ai_camera_windowing_h)
- .appendField(new Blockly.FieldNumber(224), "window_h")
- .appendField(Blockly.Msg.ai_camera_windowing_unit);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_windows_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_windows'] = function (block) {
- var w = block.getFieldValue('window_w');
- var h = block.getFieldValue('window_h');
- // var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
- var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
- return code;
- };
- Blockly.Blocks['ai_camera_fit_to_screen_size'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_fit_set);
- this.appendDummyInput()
- .appendField(new Blockly.FieldVariable("camera"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_fit_to)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_camera_fit_1, "240"],
- [Blockly.Msg.ai_camera_camera_fit_2, "224"]
- ]), "size");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_fit_to_screen_size_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_fit_to_screen_size'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_size = block.getFieldValue('size');
- // TODO: Assemble Python into code variable.
- if (dropdown_size == "240") {
- var code = 'camera = sensor.snapshot()\n' +
- variable_name + ' = ' + variable_name + '.cut(40,0,240,240)\n';
- } else if (dropdown_size == "224") {
- var code = 'camera = sensor.snapshot()\n' +
- variable_name + ' = ' + variable_name + '.cut(48,8,224,224)\n' +
- 'a = ' + variable_name + '.pix_to_ai()\n' +
- '';
- }
- return code;
- };
- Blockly.Blocks['ai_camera_snapshot'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_snapshot);
- this.setOutput(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_snapshot_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_snapshot = function (block) {
- Blockly.Python.definitions_.import_sensor = "import sensor";
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.snapshot()";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_camera_lens_corr'] = {
- init: function () {
- this.appendValueInput("lens_corr")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_camera_set_lens_corr);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_lens_corr_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_lens_corr'] = function (block) {
- var value_lens_corr = Blockly.Python.valueToCode(block, 'lens_corr', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_lens_corr + '.lens_corr(strength = 1.8, zoom = 1.0)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_camera_image_property_change'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_bcs_set_text_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_bcs_vflip, "set_vflip"],
- [Blockly.Msg.ai_camera_bcs_hmirror, "set_hmirror"],
- [Blockly.Msg.ai_camera_bcs_wb, "set_auto_whitebal"],
- [Blockly.Msg.ai_camera_bcs_g, "set_auto_gain"],
- [Blockly.Msg.ai_camera_bcs_e, "set_auto_exposure"]
- ]), "image_property")
- .appendField(Blockly.Msg.ai_camera_bcs_as_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable + Blockly.Msg.ai_camera_bcs_status, "True"],
- [Blockly.Msg.ai_camera_disable + Blockly.Msg.ai_camera_bcs_status, "False"]
- ]), "DEGREE");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('image_property');
- var TOOLTIPS = {
- 'set_vflip': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_vflip),
- 'set_hmirror': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_hmirror),
- 'set_auto_whitebal': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_wb),
- 'set_auto_gain': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_g),
- 'set_auto_exposure': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_e)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_image_property_change = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var dropdown_image_property = block.getFieldValue('image_property');
- var _code = "sensor." + dropdown_image_property + "(" + degee + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_vflip'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_vflip)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_vflip = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_vflip(" + degee + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_hmirror'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_hmirror)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_hmirror = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_hmirror(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_whitebal'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_whitebal)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_whitebal = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_whitebal(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_gain'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_gain)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_gain = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_gain(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_exposure'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_exposure)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_exposure = function (block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_exposure(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_bcs_operation'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_bcs_set_text)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_bcs_contrast, "contrast"],
- [Blockly.Msg.ai_camera_bcs_brightness, "brightness"],
- [Blockly.Msg.ai_camera_bcs_saturation, "saturation"]
- ]), "image_property")
- .appendField(Blockly.Msg.ai_camera_bcs_as);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('image_property');
- var TOOLTIPS = {
- 'contrast': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_contrast),
- 'brightness': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_brightness),
- 'saturation': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_saturation)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_bcs_operation = function (block) {
- var dropdown_image_property = block.getFieldValue('image_property');
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_" + dropdown_image_property + "(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_contrast'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_contrast_to_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_contrast = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_contrast(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_brightness'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_brightness_to_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_brightness = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_brightness(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_saturation'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_saturation_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_saturation = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_saturation(" + _x + ")\n"
- return _code;
- }
- /*
- _ ___ __ ___ _
- / \ |_ _| \ \ / (_)___(_) ___ _ __
- / _ \ | | \ \ / /| / __| |/ _ \| '_ \
- / ___ \ | | \ V / | \__ \ | (_) | | | |
- /_/ \_\___| \_/ |_|___/_|\___/|_| |_|
-
- */
- Blockly.Blocks.ai_lcd_XY_vision = {
- init: function () {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#f0983e");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.X);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.Y)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_XY_vision = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks.ai_lcd_WH_vision = {
- init: function () {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#f0983e");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.W);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.H)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_WH_vision = function (block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_vision_color_analyze'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_analyze_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_color_analyze_get_result);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_color_analyze_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_color_analyze_size);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_color_analyze_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_color_analyze'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addFunction("function", '' +
- 'def _get_regional_color_analyze_rgb(x, y, w, h):\n' +
- ' _crd_bounding_box_size = (w, h)\n' +
- ' _crd_r = [x, y, _crd_bounding_box_size[0], _crd_bounding_box_size[1]] # 50x50 center of QQVGA.\n' +
- ' _crd_hist = ' + variable_name + '.get_statistics(bins=8,roi=_crd_r)\n' +
- ' return image.lab_to_rgb((_crd_hist.l_mean(),_crd_hist.a_mean(),_crd_hist.b_mean()))\n' +
- '');
- // TODO: Assemble Python into code variable.
- var code = '_get_regional_color_analyze_rgb(' + xy + ', ' + wh + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_blobs'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_blobs_color_region);
- this.appendValueInput("color_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_color_threshold);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_size);
- /*this.appendValueInput("area_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_region_threshold)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_area");
- this.appendValueInput("pixels_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_pixel_threshold)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_pixels");
- this.appendValueInput("margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_margin)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "margin");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_find_blobs_filtered_region)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_merge");*/
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_blobs_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_blobs'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_color_threshold = Blockly.Python.valueToCode(block, 'color_threshold', Blockly.Python.ORDER_ATOMIC);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- /* var value_area_threshold = Blockly.Python.valueToCode(block, 'area_threshold', Blockly.Python.ORDER_ATOMIC);
- var value_pixels_threshold = Blockly.Python.valueToCode(block, 'pixels_threshold', Blockly.Python.ORDER_ATOMIC);
- var checkbox_check_area = block.getFieldValue('check_area') == 'TRUE';
- var checkbox_check_pixels = block.getFieldValue('check_pixels') == 'TRUE';
- var checkbox_check_merge = block.getFieldValue('check_merge') == 'TRUE';
- var value_margin = Blockly.Python.valueToCode(block, 'margin', Blockly.Python.ORDER_ATOMIC);
- var checkbox_margin = block.getFieldValue('margin') == 'TRUE';
- var area = checkbox_check_area;
- var pixels = checkbox_check_pixels;
- var margin = checkbox_margin;
- var merge = checkbox_check_merge.toString();
- var merge_state = merge.charAt(0).toUpperCase() + merge.slice(1);
- if (margin == false) {
- if (area == false && pixels == false) {
- var addtional_parameters = ' ';
- } else if (area == true && pixels == false) {
- var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold;
- } else if (area == false && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold;
- } else if (area == true && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold;
- }
- } else if (margin == true) {
- if (area == false && pixels == false) {
- var addtional_parameters = ', margin=' + value_margin;
- } else if (area == true && pixels == false) {
- var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold + ', margin=' + value_margin;
- } else if (area == false && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', margin=' + value_margin;
- } else if (area == true && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold + ', margin=' + value_margin;
- }
- }
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=' + merge_state + addtional_parameters + ')';*/
- var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=False, area_threshold=150)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_blobs'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_blobs_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_detection_box_x, "x()"],
- [Blockly.Msg.ai_vision_detection_box_y, "y()"],
- [Blockly.Msg.ai_vision_detection_box_width, "w()"],
- [Blockly.Msg.ai_vision_detection_box_height, "h()"],
- [Blockly.Msg.ai_vision_detection_box_pixels, "pixels()"],
- [Blockly.Msg.ai_vision_detection_box_cx, "cx()"],
- [Blockly.Msg.ai_vision_detection_box_cy, "cy()"],
- [Blockly.Msg.ai_vision_detection_box_degree, "rotation()"],
- [Blockly.Msg.ai_vision_detection_box_total, "count()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_blobs_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_blobs'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_threshold_default'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_detection_color_threshold_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_detection_color_threshold_set_black, "black"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_white, "white"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_red, "red"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_green, "green"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_blue, "blue"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_orange, "orange"]
- ]), "color");
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('color');
- var TOOLTIPS = {
- 'black': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_black),
- 'white': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_white),
- 'red': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_red),
- 'green': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_green),
- 'blue': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_blue),
- 'orange': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_orange)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_threshold_default'] = function (block) {
- var dropdown_color = block.getFieldValue('color');
- // TODO: Assemble Python into code variable.
- if (dropdown_color == "black") {
- var code = '[(0,40)]';
- } else if (dropdown_color == "white") {
- var code = '[(128,255)]';
- } else if (dropdown_color == "red") {
- var code = '[(45, 65, 40, 80, 40, 60)]';
- } else if (dropdown_color == "green") {
- var code = '[(45, 65, -50, -30, 0, 40)]';
- } else if (dropdown_color == "blue") {
- var code = '[(45, 65, -20, 30, -60, -20)]';
- } else if (dropdown_color == "orange") {
- var code = '[(77, 55, 19, 61, 14, 108)]';
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_qrcode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_qrcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_qrcode_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_qrcode'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_qrcodes()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_qrcode_get_info'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_qrcode_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type, "data_type"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'data_type': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
- 'x': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_qrcode_get_info'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_apriltag'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_apriltag_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_apriltag_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_apriltag_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_apriltag'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_apriltags()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_apriltag_get_info'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_apriltag_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type_id, "id"],
- [Blockly.Msg.ai_vision_pattern_detection_type_family, "family"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'id': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_id),
- 'family': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_family),
- 'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_apriltag_get_info'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_datamatrices'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_datamatrix_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_datamatrices_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_datamatrices_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_datamatrices'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_datamatrices()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_datamatrices_get_info'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_data_matrices_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_rows, "rows"],
- [Blockly.Msg.ai_vision_pattern_detection_cols, "columns"],
- [Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count, "capacity"],
- [Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count, "padding"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'rows': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_rows),
- 'columns': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_cols),
- 'capacity': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count),
- 'padding': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count),
- 'x': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_datamatrices_get_info'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_barcodes'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_barcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_barcode_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_barcodes_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_barcodes'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_barcodes()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_barcodes_get_info'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_barcode_matrices_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type, "type"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'type': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
- 'x': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_barcodes_get_info'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_circles'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_circle.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_circle);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_region_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_hoffman_threshold);
- this.appendValueInput("x_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_x);
- this.appendValueInput("y_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_y);
- this.appendValueInput("r_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_r);
- this.appendValueInput("r_min")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_min_radius);
- this.appendValueInput("r_max")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_max_radius);
- this.appendValueInput("r_step")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_step_radius);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_circles_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_circles'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- /*var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- var x_margin = Blockly.Python.valueToCode(block, 'x_margin', Blockly.Python.ORDER_ATOMIC);
- var y_margin = Blockly.Python.valueToCode(block, 'y_margin', Blockly.Python.ORDER_ATOMIC);
- var r_margin = Blockly.Python.valueToCode(block, 'r_margin', Blockly.Python.ORDER_ATOMIC);
- var r_min = Blockly.Python.valueToCode(block, 'r_min', Blockly.Python.ORDER_ATOMIC);
- var r_max = Blockly.Python.valueToCode(block, 'r_max', Blockly.Python.ORDER_ATOMIC);
- var r_step = Blockly.Python.valueToCode(block, 'r_step', Blockly.Python.ORDER_ATOMIC);*/
- // TODO: Assemble Python into code variable.
- // 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)';
- 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)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_circles'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_circle_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_circle_detected_x, "x()"],
- [Blockly.Msg.ai_vision_find_circle_detected_y, "y()"],
- [Blockly.Msg.ai_vision_find_circle_detected_radius, "r()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_circles_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_circles'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- var code = variable_name + '.' + type;
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_rects'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_rect.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_rect);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_region_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_bounding_threshold);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_rects_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_rects'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_rects(roi=(' + xy + ',' + wh + '), threshold = 10000)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_rects'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_rect_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_rect_detected_x, "x()"],
- [Blockly.Msg.ai_vision_find_rect_detected_y, "y()"],
- [Blockly.Msg.ai_vision_find_rect_detected_w, "w()"],
- [Blockly.Msg.ai_vision_find_rect_detected_h, "h()"],
- [Blockly.Msg.ai_vision_find_rect_detected_tuple, "rect()"],
- [Blockly.Msg.ai_vision_find_rect_detected_corners, "corners()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_rects_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_rects'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_lines'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_line.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_line);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_hoffman_threshold);
- this.appendValueInput("theta_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_merge_theta);
- this.appendValueInput("rho_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_merge_rho);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_lines_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_lines'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- //var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- //var theta_margin = Blockly.Python.valueToCode(block, 'theta_margin', Blockly.Python.ORDER_ATOMIC);
- //var rho_margin = Blockly.Python.valueToCode(block, 'rho_margin', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_lines(roi=(' + xy + ',' + wh + '), threshold = 1000, theta_margin = 25, rho_margin = 25)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_lines'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_line_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_line_detected_start_x, "x1()"],
- [Blockly.Msg.ai_vision_find_line_detected_start_y, "y1()"],
- [Blockly.Msg.ai_vision_find_line_detected_end_x, "x2()"],
- [Blockly.Msg.ai_vision_find_line_detected_end_y, "y2()"],
- [Blockly.Msg.ai_vision_find_line_detected_length, "length()"],
- [Blockly.Msg.ai_vision_find_line_detected_angle, "theta()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_lines_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_lines'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ __ __ _ _
- / \ |_ _| | \/ (_) ___ _ __ ___ _ __ | |__ ___ _ __ ___
- / _ \ | | | |\/| | |/ __| '__/ _ \| '_ \| '_ \ / _ \| '_ \ / _ \
- / ___ \ | | | | | | | (__| | | (_) | |_) | | | | (_) | | | | __/
- /_/ \_\___| |_| |_|_|\___|_| \___/| .__/|_| |_|\___/|_| |_|\___|
- |_|
- */
- Blockly.Blocks['ai_mphone_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_init);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.ai_mphone_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_mphone_init = function (block) {
- Blockly.Python.definitions_.import_all_mphone = "" +
- "from Maix import GPIO, I2S, FFT\n" +
- "from board import board_info\n" +
- "from fpioa_manager import fm\n" +
- "\n" +
- "sample_rate = 38640\n" +
- "sample_points = 1024\n" +
- "fft_points = 512\n" +
- "hist_x_num = 50\n" +
- "\n" +
- "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
- "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
- "\n" +
- "_recorder_rx = I2S(I2S.DEVICE_0)\n" +
- "_recorder_rx.channel_config(_recorder_rx.CHANNEL_0, _recorder_rx.RECEIVER, align_mode = I2S.STANDARD_MODE)\n" +
- "_recorder_rx.set_sample_rate(sample_rate)\n" +
- "def _microphone_read_average(lst): \n" +
- " return int((sum(lst)/len(lst))*100)\n" +
- "";
- Blockly.Python.addVariable('sample_rate', "sample_rate = 38640", true);
- Blockly.Python.addVariable('sample_points', "sample_points = 1024", true);
- Blockly.Python.addVariable('fft_points', "fft_points = 512", true);
- Blockly.Python.addVariable('hist_x_num', "hist_x_num = 50", true);
- Blockly.Python.addVariable('_recorder_rx', "", true);
- Blockly.Python.addVariable('fft_res', "", true);
- Blockly.Python.addVariable('fft_amp', "", true);
- Blockly.Python.addVariable('read_all_channel', "read_all_channel = [0, 0, 0, 0, 0, 0, 0, 0]", true);
- // Blockly.Python.addVariable('_recorder_rx', "", true);
- var _code = "" +
- "";
- return _code;
- }
- Blockly.Blocks['mphone_audio_spectrum_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_audio_spectrum.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_start_spectrum_analyze);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.mphone_audio_spectrum_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['mphone_audio_spectrum_init'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_.mphone_audio_spectrum_init_def = "" +
- "import time\n" +
- "";
- var code = "" +
- "time.sleep_ms(10)\n" +
- "_audio = _recorder_rx.record(sample_points)\n" +
- "fft_res = FFT.run(_audio.to_bytes(),fft_points)\n" +
- "fft_amp = FFT.amplitude(fft_res)\n\n" +
- "read_all_channel[0] = int(_microphone_read_average(fft_amp[0:63]))\n" +
- "read_all_channel[1] = int(_microphone_read_average(fft_amp[64:127]))\n" +
- "read_all_channel[2] = int(_microphone_read_average(fft_amp[127:191]))\n" +
- "read_all_channel[3] = int(_microphone_read_average(fft_amp[192:255]))\n" +
- "read_all_channel[4] = int(_microphone_read_average(fft_amp[256:319]))\n" +
- "read_all_channel[5] = int(_microphone_read_average(fft_amp[320:383]))\n" +
- "read_all_channel[6] = int(_microphone_read_average(fft_amp[384:447]))\n" +
- "read_all_channel[7] = int(_microphone_read_average(fft_amp[448:514]))\n" +
- "";
- return code;
- };
- Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_get_spectrum_analyze_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_1, "0"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_2, "1"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_3, "2"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_4, "3"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_5, "4"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_6, "5"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_7, "6"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_8, "7"]
- ]), "level");
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.ai_mphone_read_sound_sensitivity_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_mphone_read_sound_sensitivity'] = function (block) {
- var dropdown_level = block.getFieldValue('level');
- // TODO: Assemble Python into code variable.
- var code = 'read_all_channel[' + dropdown_level + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ _ _ _
- / \ |_ _| / \ _ _ __| (_) ___
- / _ \ | | / _ \| | | |/ _` | |/ _ \
- / ___ \ | | / ___ \ |_| | (_| | | (_) |
- /_/ \_\___| /_/ \_\__,_|\__,_|_|\___/
- */
- Blockly.Blocks['ai_audio_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_init);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- }
- };
- Blockly.Python.ai_audio_init = function (block) {
- Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
- Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
- Blockly.Python.definitions_.import_audio = "import audio";
- var _code = "";
- return _code;
- }
- Blockly.Blocks['ai_audio_play_song'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song'] = function (block) {
- var number_play_vol = block.getFieldValue('play_vol');
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "def _speaker_play_song(path, volume):\n" +
- " global _audio_play_state\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " print(\"Playing.\")\n" +
- " elif (_audio_play_state) == False:\n" +
- " print(\"Done.\")\n" +
- " break\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_state', _in, true);
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
- return _code;
- };
- Blockly.Blocks['ai_audio_play_state'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_get_play_status);
- this.setOutput(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_state'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_audio_play_state';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_play_finish'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_end_play);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_finish'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_player.finish()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_set'] = {
- init: function () {
- this.appendValueInput("Num")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_set_music_vol);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_audio_set = function (block) {
- var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
- var _code = "_player.volume(" + _num + ")\n"
- return _code;
- }
- var SPEECH_RECOGNITION_BLOCK_COLOR = "230";
- Blockly.Blocks['speech_recognition_new_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_setup_set_sensi)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_normal, "10000"],
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_high, "20000"],
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_ehigh, "30000"]
- ]), "sensitivity");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_setup'] = function (block) {
- var dropdown_sensitivity = block.getFieldValue('sensitivity');
- Blockly.Python.definitions_.speech_recognition_new_setup_def = "" +
- "import os, json, time, utime, struct\n" +
- "from Maix import I2S, GPIO\n" +
- "from modules import SpeechRecognizer\n" +
- "from fpioa_manager import *\n" +
- "fm.register(31, fm.fpioa.GPIO3)\n" +
- "fm.register(32, fm.fpioa.GPIO4)\n" +
- "\n" +
- "_led_red = GPIO(GPIO.GPIO3, GPIO.OUT)\n" +
- "_led_blue = GPIO(GPIO.GPIO4, GPIO.OUT)\n" +
- "\n" +
- "def _sr_data_save(s,content,keyword_num, model_num, path):\n" +
- " data = _s_daemon.get_model_data(keyword_num, model_num)\n" +
- " with open(path,'w') as f:\n" +
- " f.write(data)\n" +
- "\n" +
- "def _sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
- " print(path)\n" +
- " with open(path,'r') as f:\n" +
- " data = f.read()\n" +
- " _s_daemon.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
- "\n" +
- "def _sr_init_remove_old_recording():\n" +
- " global _voice_record\n" +
- " try:\n" +
- " for i in os.listdir(\"/sd/sr\"):\n" +
- " print(\"deleting \" + str(i) + \"...\")\n" +
- " os.remove(\"/sd/sr/\"+str(i))\n" +
- " print(\"file deleting done.\")\n" +
- " os.rmdir(\"/sd/sr\")\n" +
- " print(\"directory deleting done.\")\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- " except:\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- " _voice_record = True\n" +
- "\n" +
- "# Enable Microphone and Disable Wifi Feature\n" +
- "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
- "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
- "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
- "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
- "wifi_en.value(0)\n" +
- "\n" +
- "# Init recording device parameteres\n" +
- "sample_rate = 8000\n" +
- "i2s_dev = I2S(I2S.DEVICE_0)\n" +
- "# config i2s according to speechrecognizer\n" +
- "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" +
- "i2s_dev.set_sample_rate(sample_rate)\n" +
- "_s_daemon = SpeechRecognizer(i2s_dev)\n" +
- "_s_daemon.set_threshold(0,0," + dropdown_sensitivity + ")\n" +
- "";
- var code = '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recording_sample_start'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recording_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_text)
- .appendField(new Blockly.FieldNumber(2, 1, 6, 1), "clip")
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_note);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_text)
- .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "take")
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_note);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_interface)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
- [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
- [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
- ]), "interface")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recording_sample_start_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recording_sample_start'] = function (block) {
- var number_clip = block.getFieldValue('clip');
- var number_take = block.getFieldValue('take');
- var interface = block.getFieldValue('interface');
- var code = '' +
- '# Remove old recordings\n' +
- '_sr_init_remove_old_recording()\n' +
- '\n' +
- 'import lcd\n' +
- 'import image\n' +
- 'try:from cocorobo import display_cjk_string\n' +
- 'except:pass\n' +
- '\n' +
- 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n' +
- ' try:\n' +
- ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n' +
- ' return canvas\n' +
- ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n' +
- '\n' +
- '_canvas_x, _canvas_y = 0, 0\n' +
- 'lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n' +
- 'lcd.rotation(1)\n' +
- 'lcd.clear(lcd.BLACK)\n' +
- 'canvas = image.Image(size=(240, 240))\n' +
- 'canvas.clear()\n' +
- 'canvas.draw_rectangle(0,0, 240, 240, color=(0,0,0), thickness=1, fill=True)\n';
- if (interface == "English") {
- code += 'lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
- } else {
- code += 'lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
- }
- code += '\n' +
- '_voice_keyword_num = ' + number_clip + '\n' +
- '_voice_model_num = ' + number_take + '\n' +
- 'for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' 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' +
- '\n' +
- 'lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- '\n' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- 'import time\n' +
- 'from machine import Timer\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_voice_button_status = False\n' +
- '\n' +
- 'def _on_read_voice_recog_button_timer(timer):\n' +
- ' global _voice_button_status\n' +
- ' if _gp_side_a.value() == True: _voice_button_status = True\n' +
- ' elif _gp_side_a.value() == False: _voice_button_status = False\n' +
- '\n' +
- '_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' +
- '_on_read_voice_recog_periodic_tim.start()\n' +
- '\n' +
- 'try:\n' +
- ' while _voice_record == True:\n' +
- '\n' +
- ' for i in range(_voice_keyword_num):\n' +
- ' for j in range(_voice_model_num):\n' +
- ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
- '\n' +
- ' while True:\n' +
- ' if _voice_button_status == True:\n' +
- ' break\n' +
- ' else:\n' +
- ' print(".", end="")\n' +
- ' _led_red.value(1)\n' +
- ' time.sleep_ms(100)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep_ms(100)\n' +
- '\n' +
- ' # Start recoding procedure\n' +
- ' _led_red.value(0)\n' +
- ' _s_daemon.record(i, j)\n' +
- ' time.sleep_ms(500)\n' +
- '\n' +
- ' # Check recording state\n' +
- ' while (_s_daemon.get_status() != 2):\n' +
- ' if (_s_daemon.get_status() == 1):\n' +
- ' print("[CocoRobo] 现在开始说!")\n' +
- ' _led_red.value(1)\n' +
- ' _led_blue.value(1)\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 65,0, "Speak Now", color=(255,0,0), scale=2, mono_space=False)\n';
- } else if (interface == 'Simplified Chinese') {
- code += ' lcd_draw_string(canvas, 75,0, "现在开始说", font_size=1, color=(255,0,0))\n';
- } else {
- code += ' lcd_draw_string(canvas, 75,0, "現在開始說", font_size=1, color=(255,0,0))\n';
- }
- code += '\n' +
- ' if (_s_daemon.get_status() == 5):\n' +
- ' print("[CocoRobo] 收集噪音...")\n' +
- ' _led_red.value(1)\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 45,0, "Gathering Noise...", color=(255,0,0), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 75,0, "收集噪音...", font_size=1, color=(255,0,0))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' time.sleep_ms(500)\n' +
- ' if (_s_daemon.get_status() == 2):\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 95,0, "OK", color=(10,255,0), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 95,0, "成功 ", font_size=1, color=(10,255,0))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 4:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' for t in range(0,6,1):\n' +
- ' _led_blue.value(1)\n' +
- ' _led_red.value(1)\n' +
- ' time.sleep_ms(100)\n' +
- ' _led_blue.value(0)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep_ms(100)\n' +
- ' time.sleep(1)\n' +
- '\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 4:\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 4):\n' +
- ' 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' +
- ' else:\n' +
- ' 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' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- '\n' +
- ' # Done Recording\n' +
- ' content = str(i) + \'_\' + str(j)\n' +
- ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+"_" + str(_s_daemon.get_model_info(i,j)).strip("[]") +".sr"\n' +
- ' _sr_data_save(_s_daemon, content, i, j, file_name)\n' +
- ' print("frm_num is: " + str(_s_daemon.get_model_info(i,j)))\n' +
- '\n' +
- ' print("Record successful! Next, Run your speech recognition code!")\n' +
- ' _voice_record = False\n' +
- '\n' +
- ' canvas.clear()\n' +
- ' canvas.draw_image(image.Image("/sd/preset/images/tick.jpg"), 70,70, x_scale=0.5, y_scale=0.5)\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' _led_blue.value(1)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep(2)\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas,30,40, "Voice Sample", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,70, "Recording", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,100, "Failed!", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,140, "Reason: " + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- } else if (interface == 'Simplified Chinese') {
- code += ' lcd_draw_string(canvas,30,40, "语音样本",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,70, "正在记录中",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,100, "失败!",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas,30,40, "語音樣本",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,70, "正在記錄中",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,100, "失敗!",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- }
- code += '' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' _led_blue.value(0)\n' +
- ' _led_red.value(1)\n' +
- '\n' +
- 'import machine\n' +
- 'machine.reset()\n' +
- '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_start_recognition'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recognizing);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_start_recognition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_start_recognition'] = function (block) {
- Blockly.Python.definitions_.speech_recognition_new_start_recognition_def = "" +
- "import time\n\n" +
- "_voice_recording_load = True\n" +
- "_voice_recording_ready = False\n" +
- "\n" +
- "if _voice_recording_load == True:\n" +
- " _sr_filelist = os.listdir(\"/sd/sr\")\n" +
- " for _sr_file in _sr_filelist: \n" +
- " _sr_frm_num = int(_sr_file[4:_sr_file.find(\".\")])\n" +
- " print(_sr_frm_num)\n" +
- " print(\"/sd/sr/\" + str(_sr_file))\n" +
- " _sr_data_load(_s_daemon, int(_sr_file[0]), int(_sr_file[2]), _sr_frm_num, \"/sd/sr/\" + str(_sr_file))\n" +
- " print(\"load successful!\")\n" +
- "";
- var code = '_s_recognition_state = 0\n' +
- '_s_daemon.recognize()\n' +
- 'time.sleep_ms(500)\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recognition_get_any_result'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_any_result_text);
- this.appendStatementInput("NAME")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_any_result_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recognition_get_any_result'] = function (block) {
- var statements_name = Blockly.Python.statementToCode(block, 'NAME');
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'while _s_recognition_state == 0:\n' +
- ' if (_s_daemon.get_status() == 5):\n' +
- ' print("", end="")\n' +
- ' elif (_s_daemon.get_status() == 3):\n' +
- ' print("", end="")\n' +
- ' elif (_s_daemon.get_status() == 4):\n' +
- ' print("[CocoRobo] Record OK, Proceed!")\n' +
- ' _s_recognition_state = 1\n' +
- ' else:\n' +
- ' print("[CocoRobo] Current state: " + str(s.get_status()))\n' +
- '\n' +
- 'time.sleep_ms(800)\n' +
- '_s_ret = _s_daemon.get_result()\n' +
- 'print("[CocoRobo] Result: " + str(_s_ret))\n' +
- '\n' +
- 'if (_s_ret > 0):\n' +
- statements_name +
- '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recognition_get_result'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_get_result)
- .appendField(new Blockly.FieldNumber(1, 1, 6, 1), "index")
- .appendField(Blockly.Msg.ai_speech_recognition_get_result_after);
- this.setOutput(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_result_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recognition_get_result'] = function (block) {
- var number_index = block.getFieldValue('index');
- // TODO: Assemble Python into code variable.
- var code = '(_s_ret == ' + number_index + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Blocks['ai_audio_voice_recognition_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/voice_recognition_header.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField("Voice Recognition Setup");
- this.appendDummyInput()
- .appendField("Set the Total Number of Voice Clip(s): ")
- .appendField(new Blockly.FieldNumber(3, 1, 6, 1), "word_count")
- .appendField(" (No more than 6)");
- this.appendDummyInput()
- .appendField("Set the Total Count of Sample(s) for each clip: ")
- .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "repeat_count")
- .appendField(" (No more than 4)");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- var global_voice_recognition_word_count = 0;
- var global_voice_recognition_sample_count = 0;
- Blockly.Python['ai_audio_voice_recognition_setup'] = function(block) {
- var number_word_count = block.getFieldValue('word_count');
- var number_repeat_count = block.getFieldValue('repeat_count');
- // TODO: Assemble Python into code variable.
- global_voice_recognition_word_count = number_word_count;
- global_voice_recognition_sample_count = number_repeat_count;
- console.log(global_voice_recognition_word_count + "," + global_voice_recognition_sample_count);
- Blockly.Python.definitions_.voice_recognition_def = "" +
- "import lcd\n" +
- "import image\n" +
- "\n" +
- "lcd.init(type=1,freq=15000000,width=240,height=240,color=(0,0,0))\n" +
- "lcd.rotation(1)\n" +
- "lcd.clear(lcd.BLACK)\n" +
- "\n" +
- "import os, json, time, utime, struct\n" +
- "from Maix import I2S, GPIO\n" +
- "from modules import SpeechRecognizer\n" +
- "from fpioa_manager import *\n" +
- "\n" +
- "fm.register(31, fm.fpioa.GPIO3)\n" +
- "fm.register(32, fm.fpioa.GPIO4)\n" +
- "\n" +
- "led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n" +
- "led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n" +
- "\n" +
- "def sr_data_save(s,content,keyword_num, model_num, path):\n" +
- " data = s.get_model_data(keyword_num, model_num)\n" +
- " with open(path,'w') as f:\n" +
- " f.write(data)\n" +
- "\n" +
- "def sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
- " print(path)\n" +
- " with open(path,'r') as f:\n" +
- " data = f.read()\n" +
- " # print(data)\n" +
- " s.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
- "\n" +
- "def sr_led_blinking(type):\n" +
- " if type == \"waitfor\":\n" +
- " led_red.value(1)\n" +
- " time.sleep_ms(100)\n" +
- " led_red.value(0)\n" +
- " time.sleep_ms(100)\n" +
- " elif type == \"recorded\":\n" +
- " for i in range(0,5,1):\n" +
- " led_blue.value(1)\n" +
- " time.sleep_ms(100)\n" +
- " led_blue.value(0)\n" +
- " time.sleep_ms(100)\n" +
- "\n" +
- "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
- "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
- "\n" +
- "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
- "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
- "wifi_en.value(0)\n" +
- "\n" +
- "sample_rate = 8000\n" +
- "i2s_dev = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "# config i2s according to speechrecognizer\n" +
- "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" +
- "i2s_dev.set_sample_rate(sample_rate)\n" +
- "s = SpeechRecognizer(i2s_dev)\n" +
- "try:\n" +
- " print(\"Setting get_noise_threshold to less sensitive.\")\n" +
- " s.set_threshold(0,0,20000)\n" +
- "except BaseException as e:\n" +
- " print(\"Failed to set threshold, please check if your firmware is right, error info:\")\n" +
- " print(str(e))\n" +
- "";
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_start_record'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Start Recording Samples (By Pressing Button ")
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type")
- .appendField(")");
- this.appendDummyInput()
- .appendField("Note 1: Recorded Samples Will Stored in SD Card.");
- this.appendDummyInput()
- .appendField("Note 2: Write down the frm_num for each sample.");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_start_record'] = function(block) {
- var dropdown_button_type = block.getFieldValue('button_type');
- // TODO: Assemble Python into code variable.
- Blockly.Python.addVariable('_voice_on_recoding_state', '', true);
- Blockly.Python.definitions_.voice_recognition_start_record_def = "" +
- "try:\n" +
- " for i in os.listdir(\"/sd/sr\"):\n" +
- " print(\"deleting \" + str(i) + \"...\")\n" +
- " os.remove(\"/sd/sr/\"+str(i))\n" +
- " print(\"file deleting done.\")\n" +
- " os.rmdir(\"/sd/sr\")\n" +
- " print(\"directory deleting done.\")\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- "except:\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- "\n" +
- "_voice_record = True\n" +
- "\n" +
- "from fpioa_manager import *\n" +
- "from Maix import FPIOA, GPIO\n" +
- "\n" +
- "_gp_side_buttons = [9, 10, 11]\n" +
- "\n" +
- "FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n" +
- "FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n" +
- "FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n" +
- "\n" +
- "_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n" +
- "_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n" +
- "_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n" +
- "\n" +
- "import time\n" +
- "from machine import Timer\n" +
- "\n" +
- "_voice_button_status = False\n" +
- "\n" +
- "def _on_read_voice_recog_button_timer(timer):\n" +
- " global _voice_button_status\n" +
- " if _gp_side_" + dropdown_button_type + ".value() == True: _voice_button_status = True\n" +
- " elif _gp_side_" + dropdown_button_type + ".value() == False: _voice_button_status = False\n" +
- "\n" +
- "_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" +
- "_on_read_voice_recog_periodic_tim.start()\n" +
- "";
- var code = '' +
- 'while _voice_record == True:\n' +
- ' print(str(_voice_button_status))\n' +
- '\n' +
- ' _voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords, \n' +
- ' _voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
- '\n' +
- ' for i in range(_voice_keyword_num):\n' +
- ' for j in range(_voice_model_num):\n' +
- ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
- ' while True:\n' +
- ' if _voice_button_status == True:\n' +
- ' break\n' +
- ' else:\n' +
- ' print(\'.\', end="")\n' +
- ' sr_led_blinking("waitfor")\n' +
- ' led_red.value(1)\n' +
- ' s.record(i, j)\n' +
- ' led_red.value(0)\n' +
- ' sr_led_blinking("recorded")\n' +
- ' content = str(i) + \'_\' + str(j)\n' +
- ' print(content)\n' +
- ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+".sr"\n' +
- ' b = sr_data_save(s, content, i, j, file_name)\n' +
- ' print(b)\n' +
- ' print("Record successful! Next, Run your speech recognition code!")\n' +
- '\n' +
- ' _voice_record = False\n' +
- '';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_start_recognizing'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Start Recognizing");
- this.appendValueInput("frm_num_set")
- .setCheck(null)
- .appendField("Set Frame Number for Each Sample:");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_start_recognizing'] = function(block) {
- var value_frm_num_set = Blockly.Python.valueToCode(block, 'frm_num_set', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var sr_data_load_content = '';
- var count = 0;
- for (var i = 0; i < global_voice_recognition_word_count; i++) {
- console.log(i);
- for (var j = 0; j < global_voice_recognition_sample_count; j++) {
- console.log(j);
- sr_data_load_content += ' sr_data_load(s, ' + i + ', ' + j + ', _voice_recording_load_frm_num[' + i + '][' + j + '], "/sd/sr/' + i + '_' + j + '.sr")\n'
- count += 1;
- }
- }
- Blockly.Python.definitions_.voice_recognition_start_recognizing_def = '' +
- '_voice_recording_load = True\n' +
- '_voice_recording_ready = False\n' +
- '\n' +
- '_voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords\n' +
- '_voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
- '\n' +
- '_voice_recording_load_frm_num = [0] * (_voice_keyword_num*_voice_model_num)\n' +
- '_voice_recording_load_frm_num = ' + value_frm_num_set + '\n' +
- '\n' +
- 'if _voice_recording_load == True:\n' +
- sr_data_load_content +
- // ' sr_data_load(s, 0, 0, 86, "/sd/sr/0_0.sr")\n' +
- // ' sr_data_load(s, 0, 1, 43, "/sd/sr/0_1.sr")\n' +
- // ' sr_data_load(s, 0, 2, 45, "/sd/sr/0_2.sr")\n' +
- // ' sr_data_load(s, 1, 0, 39, "/sd/sr/1_0.sr")\n' +
- // ' sr_data_load(s, 1, 1, 82, "/sd/sr/1_1.sr")\n' +
- // ' sr_data_load(s, 1, 2, 134, "/sd/sr/1_2.sr")\n' +
- // ' sr_data_load(s, 2, 0, 35, "/sd/sr/2_0.sr")\n' +
- // ' sr_data_load(s, 2, 1, 158, "/sd/sr/2_1.sr")\n' +
- // ' sr_data_load(s, 2, 2, 87, "/sd/sr/2_2.sr")\n' +
- ' print("load successful!")\n' +
- '';
- var code = '_voice_recognition_result = s.recognize()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_status'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("When Get Noise is Done");
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_status'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '...';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_any_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Get Any Result from Voice Recogntion")
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_any_result'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_voice_recognition_result > 0';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("When Recognized Clip #")
- .appendField(new Blockly.FieldNumber(0, 1, 6), "clip_no");
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_result'] = function(block) {
- var dropdown_clip_no = block.getFieldValue('clip_no');
- // TODO: Assemble Python into code variable.
- var code = '_voice_recognition_result == ' + dropdown_clip_no;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- */
- /*
- _ ___ ____ _
- / \ |_ _| / ___| _ __ ___ __ _| | _____ _ __
- / _ \ | | \___ \| '_ \ / _ \/ _` | |/ / _ \ '__|
- / ___ \ _ | | _ ___) | |_) | __/ (_| | < __/ |
- /_/ \_(_)___(_) |____/| .__/ \___|\__,_|_|\_\___|_|
- |_|
- */
- Blockly.Blocks['ai_audio_init_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_init);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_init_speaker_TOOLTIP);
- this.setHelpUrl("");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- }
- };
- Blockly.Python.ai_audio_init_speaker = function (block) {
- Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
- Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
- Blockly.Python.definitions_.import_audio = "import audio";
- var _code = "";
- return _code;
- }
- Blockly.Blocks['ai_audio_play_song_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("volume")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song_speaker'] = function (block) {
- // var number_play_vol = block.getFieldValue('play_vol');
- var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "def _speaker_play_song(path, volume):\n" +
- " global _audio_play_state,_player\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " try:\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " print(\"Playing.\")\n" +
- " elif (_audio_play_state) == False:\n" +
- " print(\"Done.\")\n" +
- " break\n" +
- "\n" +
- " except:\n" +
- " pass\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_state', _in, true);
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
- return _code;
- };
- Blockly.Blocks['ai_audio_play_state_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_get_play_status);
- this.setOutput(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_state_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_state_speaker'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_audio_play_state';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_play_finish_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_end_play);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_finish_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_finish_speaker'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_player.finish()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_set_speaker'] = {
- init: function () {
- this.appendValueInput("Num")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_set_music_vol);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_set_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_audio_set_speaker = function (block) {
- var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
- var _code = "_player.volume(" + _num + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_audio_files_list'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_files_list);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_files_list_set_file_path);
- this.setOutput(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_files_list_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_audio_files_list = function (block) {
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.import_ai_audio_files_list = '' +
- 'import os\n'+
- '\n' +
- 'def getRawFileList(path):\n' +
- ' files = []\n' +
- ' for f in os.listdir(path):\n' +
- ' if (not f.endswith("~") or not f == "") and f.split(".")[1] == "wav":\n' +
- ' files.append(path + "/" + f)\n' +
- ' return files\n' +
- '\n';
- var code = 'getRawFileList(' + value_name +')';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_play_song_list_loop_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file_list_loop);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("volume")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.appendValueInput("my_list")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
- this.appendValueInput("firstIndex")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_list_loop_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song_list_loop_speaker'] = function (block) {
- // var number_play_vol = block.getFieldValue('play_vol');
- var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
- var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "# 上一首\n" +
- "def previous_song(index,my_list):\n"+
- " global direction\n" +
- " index = index - 1\n"+
- " direction = 1\n" +
- " if index == -1:\n"+
- " index = len(my_list) - 1\n"+
- " return index\n" +
- "\n"+
- "# 切换上一首\n" +
- "def change_previous_song(index):\n"+
- " global _audio_play_state,direction,firstIndex,_player\n" +
- " _player.finish()\n" +
- " _audio_play_state = 0\n" +
- " firstIndex = index\n" +
- " direction = 1\n" +
- "\n"+
- "# 下一首\n" +
- "def next_song(index,my_list):\n"+
- " global direction\n" +
- " index = index + 1\n"+
- " direction = 2\n" +
- " if index == len(my_list):\n" +
- " index = 0\n" +
- " return index\n" +
- "\n"+
- "# 切换下一首\n" +
- "def change_next_song(index):\n"+
- " global _audio_play_state,direction,firstIndex,_player\n" +
- " _player.finish()\n" +
- " _audio_play_state = 0\n" +
- " firstIndex = index\n" +
- " direction = 2\n" +
- "\n"+
- "def _speaker_play_song_list_loop(path, volume, my_list, index):\n" +
- " global _audio_play_state,_player,firstIndex,play_again,direction\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " try:\n" +
- " firstIndex = index\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " button_a_state = False\n" +
- " button_b_state = False\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " #print(\"Playing.\")\n" +
- " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
- " index = previous_song(index,my_list)\n" +
- " button_a_state = True\n" +
- " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
- " button_a_state = False\n" +
- " change_previous_song(index)\n" +
- " break\n" +
- " if (_gp_side_b.value() == 1) and button_b_state == False:\n" +
- " index = next_song(index,my_list)\n"+
- " button_b_state = True\n" +
- " elif (_gp_side_b.value() == 0) and button_b_state == True:\n" +
- " button_b_state = False\n" +
- " change_next_song(index)\n" +
- " break\n" +
- "\n" +
- " elif (_audio_play_state) == False:\n" +
- " #print(\"Done.\")\n" +
- " index = index + 1\n" +
- " if index == len(my_list):\n" +
- " index = 0\n" +
- " firstIndex = index\n" +
- " break\n" +
- "\n" +
- " except:\n" +
- " play_again = True\n" +
- " if direction == 0:\n" +
- " direction = 3\n" +
- " return\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_list_loop_state', _in, true);
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song_list_loop(path=" + value_name + ", volume=" + number_play_vol + ", my_list=" + my_list + ", index=" + firstIndex + ")\n";
- return _code;
- };
- Blockly.Blocks['ai_audio_play_song_list_random_speaker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file_list_random);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("volume")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.appendValueInput("my_list")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
- this.appendValueInput("firstIndex")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_music_file_list_random_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song_list_random_speaker'] = function (block) {
- // var number_play_vol = block.getFieldValue('play_vol');
- var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
- var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "import time\n" +
- "import random\n" +
- "\n" +
- "random.seed(time.ticks_cpu())\n" +
- "\n" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "# 随机一首\n" +
- "def random_song(index,my_list):\n"+
- " index = random.randrange(0, len(my_list), 1)\n"+
- " return index\n" +
- "\n"+
- "# 切换上一首\n" +
- "def change_random_song(index):\n"+
- " global _audio_play_state,firstIndex,_player\n" +
- " _player.finish()\n" +
- " _audio_play_state = 0\n" +
- " firstIndex = index\n" +
- "\n"+
- "def _speaker_play_song_list_random(path, volume, my_list, index):\n" +
- " global _audio_play_state,_player,firstIndex,play_again\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " try:\n" +
- " firstIndex = index\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " button_a_state = False\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " #print(\"Playing.\")\n" +
- " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
- " index = random_song(index,my_list)\n" +
- " button_a_state = True\n" +
- " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
- " button_a_state = False\n" +
- " change_random_song(index)\n" +
- " break\n" +
- "\n" +
- " elif (_audio_play_state) == False:\n" +
- " #print(\"Done.\")\n" +
- " index = random_song(index,my_list)\n" +
- " firstIndex = index\n" +
- " break\n" +
- "\n" +
- " except:\n" +
- " play_again = True\n" +
- " return\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_list_random_state', _in, true);
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song_list_random(path=" + value_name + ", volume=" + number_play_vol + ", my_list=" + my_list + ", index=" + firstIndex + ")\n";
- return _code;
- };
- /*
- _ ___ __ ___ _
- / \ |_ _| \ \ / (_) __| | ___ ___
- / _ \ | | \ \ / /| |/ _` |/ _ \/ _ \
- / ___ \ | | \ V / | | (_| | __/ (_) |
- /_/ \_\___| \_/ |_|\__,_|\___|\___/
- */
- Blockly.Blocks['ai_video_record_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_start_recording);
- this.appendValueInput("save_path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_file_path);
- this.appendValueInput("w")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_width);
- this.appendValueInput("h")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_height);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_record_setup'] = function (block) {
- var value_save_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
- var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
- var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.import_video = "import video";
- Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
- Blockly.Python.addVariable('record_vid', 'record_vid = None', true);
- // TODO: Assemble Python into code variable.
- var code = "_record_vid = video.open(" + value_save_path + ", record=1, width=" + value_w + ", height=" + value_h + ", interval=20000, quality=80)\n";
- return code;
- };
- Blockly.Blocks['ai_video_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_to_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_video_record);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_get = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "_record_vid.record(" + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_video_stop'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_stop_recording);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_stop = function (block) {
- var _code = "_record_vid.record_finish()\n"
- return _code;
- }
- Blockly.Blocks['ai_video_set'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_play_video);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_play_video_file_path);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_play_video_2);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_set_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_set'] = function (block) {
- Blockly.Python.definitions_.import_video = "import video";
- Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
- Blockly.Python.addVariable('_video_play', '', true);
- var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var code = '_video_play = video.open(' + value_path + ')\n';
- return code;
- };
- Blockly.Blocks['ai_video_stop_play'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_stop_video_playing);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_stop_play_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_stop_play'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_video_play.__del__()\n';
- return code;
- };
- Blockly.Blocks['ai_video_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_get_video_playing_status);
- this.setOutput(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_status = function (block) {
- var code = "_video_play.play()";
- return [code, Blockly.Python.ORDER_NONE];
- }
- /*
- _ ___ __ __ _ _
- / \ |_ _| | \/ | ___ __| | ___| |___
- / _ \ | | | |\/| |/ _ \ / _` |/ _ \ / __|
- / ___ \ | | | | | | (_) | (_| | __/ \__ \
- /_/ \_\___| |_| |_|\___/ \__,_|\___|_|___/
- */
- Blockly.Blocks['ai_model_allinit'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_init_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
- [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
- [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
- ]), "DEGREE");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_init_source)
- .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('DEGREE');
- var TOOLTIPS = {
- 'Single Digit Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
- 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
- 'Human Face Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_allinit = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var degee = block.getFieldValue('DEGREE');
- var _code = ""
- if (degee == "Single Digit Recognition") {
- Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
- // Blockly.Python.addVariable('img_mnist1', '', true);
- // Blockly.Python.addVariable('img_mnist_resized', '', true);
- _code += "img_mnist1=img_mnist.to_grayscale(1)\n";
- _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
- _code += "a=img_mnist_resized.invert()\n";
- _code += "a=img_mnist_resized.strech_char(1)\n";
- _code += "a=img_mnist_resized.pix_to_ai()\n";
- _code += "fmap_mnist=kpu.forward(task_mnist, " + variable_name + ")\n";
- _code += "plist_mnist=fmap_mnist[:]\n";
- _code += "pmax_mnist=max(plist_mnist)\n";
- _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
- } else if (degee == "Common Object Recognition") {
- Blockly.Python.addVariable('task_objectrecognition', '', true);
- Blockly.Python.addVariable('img_objectrecognition', '' +
- 'classes_objectrecognition = [\'aeroplane\', \'bicycle\', \'bird\', \'boat\', \'bottle\', \'bus\', \'car\', \'cat\', \'chair\', \'cow\', \'diningtable\', \'dog\', \'horse\', \'motorbike\', \'person\', \'pottedplant\', \'sheep\', \'sofa\', \'train\', \'tvmonitor\']\n' +
- 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
- 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
- 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
- '', true);
- // Blockly.Python.addVariable('code_objectrecognition', '', true);
- _code += "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n";
- _code += variable_name + ".ai_to_pix()\n";
- } else {
- Blockly.Python.addVariable('task_facerecognition', '', true);
- // Blockly.Python.addVariable('task_facerecognition', '', true);
- Blockly.Python.addVariable('img_facerecognition', "" +
- "task_facerecognition = kpu.load(\"/sd/preset/models/preset/face-recognition.kmodel\")\n" +
- "anchor_face = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n" +
- "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor_face)\n" +
- "\n", true);
- // Blockly.Python.addVariable('code_facerecognition', '', true);
- _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
- _code += variable_name + ".ai_to_pix()\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_model_index_mnist'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_hand_written_model_result);
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_hand_written_model_number, "c"],
- [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"]
- ]), "value");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_index_mnist_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_index_mnist = function (block) {
- var value = block.getFieldValue('value');
- var _code = "max_index_mnist";
- if (value == "confidence") {
- _code = "int(pmax_mnist*100)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_look'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_object_model_if);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_look_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_look = function (block) {
- var _code = "code_objectrecognition";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_object_model_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_object_model_name, "Object"],
- [Blockly.Msg.ai_models_object_model_x, "X"],
- [Blockly.Msg.ai_models_object_model_y, "Y"],
- [Blockly.Msg.ai_models_object_model_w, "W"],
- [Blockly.Msg.ai_models_object_model_h, "H"],
- [Blockly.Msg.ai_models_object_model_x_center, "CX"],
- [Blockly.Msg.ai_models_object_model_y_center, "CY"],
- [Blockly.Msg.ai_models_object_model_count, "Number"],
- [Blockly.Msg.ai_models_object_model_index, "Serial"],
- [Blockly.Msg.ai_models_object_model_confidence, "Confidence"]
- ]), "TYPE");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('TYPE');
- var TOOLTIPS = {
- 'Object': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_name),
- 'X': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x),
- 'Y': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y),
- 'W': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_w),
- 'H': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_h),
- 'CX': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x_center),
- 'CY': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y_center),
- 'Number': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_count),
- 'Serial': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_index),
- 'Confidence': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_get = function (block) {
- var type = block.getFieldValue('TYPE');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "";
- if (type == "Object") {
- _code = "classes_objectrecognition[" + variable_name + ".classid()]";
- } else if (type == "X") {
- _code = variable_name + ".x()";
- } else if (type == "Y") {
- _code = variable_name + ".y()";
- } else if (type == "W") {
- _code = variable_name + ".w()";
- } else if (type == "H") {
- _code = variable_name + ".h()";
- } else if (type == "Number") {
- _code = variable_name + ".objnum()";
- } else if (type == "Serial") {
- _code = variable_name + ".index()";
- } else if (type == "Confidence") {
- _code = variable_name + ".value()";
- } else if (type == "CX") {
- _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (type == "CY") {
- _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_face'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_face_model_if);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_face_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_face = function (block) {
- var _code = "code_facerecognition";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_xy'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_face_model_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_face_model_x, "X"],
- [Blockly.Msg.ai_models_face_model_y, "Y"],
- [Blockly.Msg.ai_models_face_model_w, "W"],
- [Blockly.Msg.ai_models_face_model_h, "H"],
- [Blockly.Msg.ai_models_face_model_x_center, "CX"],
- [Blockly.Msg.ai_models_face_model_y_center, "CY"],
- [Blockly.Msg.ai_models_face_model_count, "Number"],
- [Blockly.Msg.ai_models_face_model_index, "Serial"],
- [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
- ]), "TYPE");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('TYPE');
- var TOOLTIPS = {
- 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
- 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
- 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
- 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
- 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
- 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
- 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
- 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
- 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_xy = function (block) {
- var type = block.getFieldValue('TYPE');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "";
- if (type == "X") {
- _code = variable_name + ".x()";
- } else if (type == "Y") {
- _code = variable_name + ".y()";
- } else if (type == "W") {
- _code = variable_name + ".w()";
- } else if (type == "H") {
- _code = variable_name + ".h()";
- } else if (type == "Number") {
- _code = variable_name + ".objnum()";
- } else if (type == "Serial") {
- _code = variable_name + ".index()";
- } else if (type == "Confidence") {
- _code = variable_name + ".value()";
- } else if (type == "CX") {
- _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (type == "CY") {
- _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_model_customized_init_setup_definition'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_customized_init_setup_definition);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_init_setup_definition'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.includes_.import_amxisd = '' +
- 'print("Clearing Cached Variables...", end="")\n' +
- 'for name in dir(): \n' +
- ' if not name.startswith(\'_\'): \n' +
- ' del globals()[name]\n' +
- 'print(" Done")\n' +
- 'import KPU as kpu\n' +
- 'kpu.memtest()\n' +
- 'from Maix import utils\n' +
- 'import gc\n' +
- 'gc.enable()\n' +
- 'utils.gc_heap_size()\n' +
- '\n' +
- '################# Done Init #################\n' +
- '';
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_model_customized_init_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
- .appendField(new Blockly.FieldTextInput("/sd/user/mymodel.kmodel"), "model_path");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
- .appendField(new Blockly.FieldVariable("camera"), "varitem");
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_init_setup'] = function (block) {
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var text_model_path = block.getFieldValue('model_path');
- Blockly.Python.definitions_.import_KPU_customized = "import KPU as kpu";
- Blockly.Python.addVariable('task_customized_model', '' +
- 'task_customized_model = kpu.load("' + text_model_path + '")\n' +
- 'anchor_customized_model = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828)\n' +
- 'a = kpu.init_yolo2(task_customized_model, 0.6, 0.3, 5, anchor_customized_model)\n' +
- '', true);
- Blockly.Python.addVariable('classes_customized_model', '' +
- 'classes_customized_model = ' + value_class_input + '\n' +
- '', true);
- var code = 'code_customized_model = kpu.run_yolo2(task_customized_model, ' + variable_name + ')\n';
- return code;
- };
- Blockly.Blocks['ai_model_face_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_face);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_type_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
- [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
- ]), "detect_type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
- .appendField(new Blockly.FieldVariable("img"), "varitem");
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_model_face_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_face_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_face_init'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- var dropdown_detect_type = block.getFieldValue('detect_type');
- if ( dropdown_detect_type == "fast" ) {
- var break_string = "break\n";
- } else if ( dropdown_detect_type == "slow" ) {
- var break_string = "# break\n";
- }
- Blockly.Python.includes_.import_modelface = `
- for name in dir():
- if not name.startswith('_'):
- del globals()[name]
- import gc
- import KPU as kpu
- from Maix import utils
- utils.gc_heap_size(0xAF000)
- import sensor
- import image
- import lcd
- import time
- from Maix import FPIOA, GPIO
- import gc
- from fpioa_manager import fm
- from board import board_info
- try:from cocorobo import display_cjk_string
- except:pass
- def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
- try:
- display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
- return canvas
- except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
- Blockly.Python.addFunction("set_key_state", `def set_key_state(*_):
- global start_processing
- start_processing = True`);
- Blockly.Python.addVariable('task_f', `
- gc.enable()
- gc.collect()
- # kpu.memtest()
- task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel")
- task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel")
- task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel")
- #kpu.set_outputs(task, 0, 1,1,1) # 配置输出形状
- kpu.set_outputs(task_fe, 0, 1,1,512)
- # kpu.memtest()
- kpu.set_outputs(task_fe, 0,1,1, 512)
- fm.register(10, fm.fpioa.GPIOHS0)
- key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
- start_processing = False
- key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
- lcd.init(type=2)
- lcd.rotation(1)
- sensor.reset()
- sensor.set_pixformat(sensor.RGB565)
- sensor.set_framesize(sensor.QVGA)
- sensor.set_vflip(0)
- sensor.set_hmirror(0)
- sensor.run(1)
- 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
- dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)] # standard face key point position
- a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
- img_lcd = image.Image()
- img_face = image.Image(size=(128, 128))
- index = -1
- a = img_face.pix_to_ai()
- record_ftr = []
- record_ftrs = []
- gc.collect()
- names = ` + value_class_input + `
- `, true);
- var code = variable_name + ` = sensor.snapshot()
- code = kpu.run_yolo2(task_fd, ` + variable_name + `)
- index = -1
- if code:
- for i in code:
- # Cut face and resize to 128x128
- a = ` + variable_name + `.draw_rectangle(i.rect())
- face_cut = ` + variable_name + `.cut(i.x(), i.y(), i.w(), i.h())
- face_cut_128 = face_cut.resize(128, 128)
- a = face_cut_128.pix_to_ai()
- #a = ` + variable_name + `.draw_image(face_cut_128, (0,0))
- # Landmark for face 5 points
- fmap = kpu.forward(task_ld, face_cut_128)
- plist = fmap[:]
- le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
- re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
- nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
- lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
- rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
- a = ` + variable_name + `.draw_circle(le[0], le[1], 4)
- a = ` + variable_name + `.draw_circle(re[0], re[1], 4)
- a = ` + variable_name + `.draw_circle(nose[0], nose[1], 4)
- a = ` + variable_name + `.draw_circle(lm[0], lm[1], 4)
- a = ` + variable_name + `.draw_circle(rm[0], rm[1], 4)
- # align face to standard position
- src_point = [le, re, nose, lm, rm]
- T = image.get_affine_transform(src_point, dst_point)
- a = image.warp_affine_ai(` + variable_name + `, img_face, T)
- a = img_face.ai_to_pix()
- #a = ` + variable_name + `.draw_image(img_face, (128,0))
- del(face_cut_128)
- # calculate face feature vector
- fmap = kpu.forward(task_fe, img_face)
- feature = kpu.face_encode(fmap[:])
- reg_flag = False
- scores = []
- for j in range(len(record_ftrs)):
- score = kpu.face_compare(record_ftrs[j], feature)
- scores.append(score)
- max_score = 0
- index = -1
- for k in range(len(scores)):
- if max_score < scores[k]:
- max_score = scores[k]
- index = k
- if(len(names) > index):
- if max_score > 85:
- 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)
- else:
- a = lcd_draw_string(` + variable_name + `, i.x(), i.y(), ("X :%2.1f" % (max_score)), color=(255, 0, 0), scale=2, mono_space=False)
- if start_processing:
- record_ftr = feature
- record_ftrs.append(record_ftr)
- start_processing = False
- `+ break_string +
- variable_name + ` = ` + variable_name + `.cut(40,0,240,240)
- a = lcd.display(` + variable_name + `, oft=(0,0))
- gc.collect()
- # kpu.memtest()
- `;
- return code;
- };
- Blockly.Blocks['ai_model_class'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_class);
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_model_class_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_class_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_class'] = function (block) {
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.import_modelface = `import KPU as kpu
- import sensor
- import lcd
- from Maix import FPIOA, GPIO
- from fpioa_manager import fm, board_info
- import time
- import gc
- try:from cocorobo import display_cjk_string
- except:pass
- def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
- try:
- display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
- return canvas
- except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
- Blockly.Python.addFunction("draw_string", `def draw_string(img, x, y, text, color, scale, bg=None ):
- if bg:
- img.draw_rectangle(x-2,y-2, len(text)*10*scale+4 , 24*scale, fill=True, color=bg)
- image = lcd_draw_string(img, x, y, text, color=color,scale=scale*2,mono_space=False)
- return image
- `);
- Blockly.Python.addVariable('class_', `############### config #################
- class_names = ` + value_class_input + `
- class_num = len(class_names)
- sample_num = len(class_names) * 5
- THRESHOLD = 11
- board_cube = 0
- button_state = False
- ########################################
- lcd.init(type=2)
- lcd.rotation(1)
- sensor.reset()
- sensor.set_pixformat(sensor.RGB565)
- sensor.set_framesize(sensor.QVGA)
- sensor.set_vflip(0)
- sensor.set_hmirror(0)
- sensor.run(1)
- #sensor.reset()
- #sensor.set_pixformat(sensor.RGB565)
- #sensor.set_framesize(sensor.QVGA)
- #sensor.set_windowing((224, 224))
- #if board_cube == 1:
- # sensor.set_vflip(True)
- # sensor.set_hmirror(True)
- # lcd.init(type=2)
- # lcd.rotation(2)
- #else:
- # lcd.init()
- #fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
- #key = GPIO(GPIO.GPIOHS0, GPIO.PULL_UP)
- FPIOA().set_function(10, FPIOA.GPIO1)
- key1 = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)
- try:
- del model
- except Exception:
- pass
- try:
- del classifier
- except Exception:
- pass
- gc.collect()
- model = kpu.load("/sd/preset/models/object_classifier.emodel")
- classifier = kpu.classifier(model, class_num, sample_num)
- cap_num = 0
- train_status = 0
- last_cap_time = 0
- last_btn_status = 0
- res_index = -1
- `)
- var code = `img = sensor.snapshot()
- if board_cube:
- img = img.rotation_corr(z_rotation=90)
- img.pix_to_ai()
- # capture img
- if train_status == 0:
- if key1.value() == 0 and last_btn_status == 1:
- #time.sleep_ms(30)
- #if key1.value() == 1 and (last_btn_status == 1) and (time.ticks_ms() - last_cap_time > 500):
- last_btn_status = 0
- last_cap_time = time.ticks_ms()
- if cap_num < class_num:
- index = classifier.add_class_img(img)
- cap_num += 1
- elif cap_num < class_num + sample_num:
- index = classifier.add_sample_img(img)
- cap_num += 1
- #else:
- # img = draw_string(img, 2, 200, "release boot key please", color=lcd.WHITE,scale=1, bg=lcd.RED)
- else:
- #time.sleep_ms(30)
- if key1.value() == 1 and (last_btn_status == 0):
- last_btn_status = 1
- if cap_num < class_num:
- img = draw_string(img, 0, 2, "press right key to cap "+class_names[cap_num], color=lcd.WHITE,scale=1, bg=lcd.RED)
- elif cap_num < class_num + sample_num:
- img = draw_string(img, 0, 2, "right key to cap sample{}".format(cap_num-class_num), color=lcd.WHITE,scale=1, bg=lcd.RED)
- # train and predict
- if train_status == 0:
- if cap_num >= class_num + sample_num:
- img = draw_string(img, 30, 100, "training...", color=lcd.WHITE,scale=1, bg=lcd.RED)
- lcd.display(img)
- classifier.train()
- train_status = 1
- else:
- res_index = -1
- try:
- res_index, min_dist = classifier.predict(img)
- except Exception as e:
- print("predict err:", e)
- if res_index >= 0 and min_dist < THRESHOLD :
- img = draw_string(img, 2, 2, class_names[res_index], color=lcd.WHITE,scale=1, bg=lcd.RED)
- else:
- img = draw_string(img, 2, 2, 'maybe {}'.format(class_names[res_index]), color=lcd.WHITE,scale=1, bg=lcd.RED)
- img = img.cut(0,0,240,240)
- lcd.display(img, oft=(0,0))
- `;
- return code;
- };
- Blockly.Blocks['ai_model_getface'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_getface);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_getface_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_getface'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'names[index] if (index != -1 and len(names) > index) else ""';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_getclass'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_getclass);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_getclass_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_getclass'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'class_names[res_index] if (res_index != -1 and len(class_names) > res_index) else ""';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_customized_if_got'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_if_got);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_if_got_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_if_got'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = 'code_customized_model';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_customized_get_result'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_customized_get_result_any)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_customized_get_result_name, "classid"],
- [Blockly.Msg.ai_models_customized_get_result_x, "x"],
- [Blockly.Msg.ai_models_customized_get_result_y, "y"],
- [Blockly.Msg.ai_models_customized_get_result_w, "w"],
- [Blockly.Msg.ai_models_customized_get_result_h, "h"],
- [Blockly.Msg.ai_models_customized_get_result_x_center, "cx"],
- [Blockly.Msg.ai_models_customized_get_result_y_center, "cy"],
- [Blockly.Msg.ai_models_customized_get_result_count, "objnum"],
- [Blockly.Msg.ai_models_customized_get_result_index, "index"],
- [Blockly.Msg.ai_models_customized_get_result_confidence, "value"]
- ]), "get_results");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('get_results');
- var TOOLTIPS = {
- 'classid': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_name),
- 'x': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x),
- 'y': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y),
- 'w': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_w),
- 'h': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_h),
- 'cx': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x_center),
- 'cy': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y_center),
- 'objnum': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_count),
- 'index': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_index),
- 'value': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_get_result'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_get_results = block.getFieldValue('get_results');
- // TODO: Assemble Python into code variable.
- if (dropdown_get_results == "classid") {
- var code = 'classes_customized_model[' + variable_name + '.classid()]';
- } else if ((dropdown_get_results != "classid") && (dropdown_get_results != "cx") && (dropdown_get_results != "cy")) {
- var code = variable_name + '.' + dropdown_get_results + '()';
- } else if (dropdown_get_results == "cx") {
- var code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (dropdown_get_results == "cy") {
- var code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Blocks['ai_model_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.MODELLPM)
- .appendField(new Blockly.FieldDropdown([
- ["Single Digit Recognition", "Single Digit Recognition"],
- ["Common Object Recognition", "Common Object Recognition"],
- ["Animal Recognition", "Animal Recognition"],
- ["Human Face Recognition", "Human Face Recognition"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_init = function(block) {
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var degee = block.getFieldValue('DEGREE');
- var _code = ""
- if (degee == "Single Digit Recognition") {
- _code += "task_mnist = kpu.load(\"/sd/models/mnist.kmodel\")\n";
- } else if (degee == "Common Object Recognition") {
- _code += "classes_animalrecognition = ['aeroplane', 'bicycle', '', 'boat', 'bottle', 'bus', 'car', '', 'chair', '', 'diningtable', '', '', 'motorbike', 'person', 'pottedplant', '', 'sofa', 'train', 'tvmonitor']\n";
- _code += "task_animalrecognition = kpu.load(\"/sd/models/object-recognition.kmodel\")\n";
- _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
- _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
- } else if (degee == "Animal Recognition") {
- _code += "classes_animalrecognition = ['', '', 'bird', '', '', '', '', 'cat', '', 'cow', '', 'dog', 'horse', '', '', '', 'sheep', '', '', '']\n";
- _code += "task_animalrecognition = kpu.load(\"/sd/models/animal-recognition.kmodel\")\n";
- _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
- _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
- } else {
- _code += "task_facerecognition = kpu.load(\"/sd/models/face-recognition.kmodel\")\n";
- _code += "anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n";
- _code += "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor)\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_model_img_mnist'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_SISF)
- .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_img_mnist = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "img_mnist1=" + variable_name + ".to_grayscale(1)\n"
- _code += "img_mnist2=img_mnist1.resize(28,28)\n"
- _code += "a=img_mnist2.invert()\n"
- _code += "a=img_mnist2.strech_char(1)\n"
- _code += "a=img_mnist2.pix_to_ai()\n"
- _code += "fmap_mnist=kpu.forward(task_mnist,img_mnist2)\n"
- _code += "plist_mnist=fmap_mnist[:]\n"
- _code += "pmax_mnist=max(plist_mnist)\n"
- _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n"
- return _code;
- }
- */
- /*
- Blockly.Blocks['ai_model_pmax_mnist'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_GTCS)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_pmax_mnist = function(block) {
- var _code = "int(pmax_mnist*100)\n"
- return _code;
- }
- Blockly.Blocks['ai_model_objectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
- .appendField(new Blockly.FieldVariable("img_objectrecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_objectrecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_aobjectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_ACOID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_aobjectrecognition = function(block) {
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_ifaobjectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_SETROTATION)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_ifaobjectrecognition = function(block) {
- // if code_objectrecognition: # if object is detected then do
- // for i_objectrecognition in code_objectrecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyobjectreocgnition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPM_GIOTCOD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyobjectreocgnition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_objectreocgnition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_cobjectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_DCOIA)
- .appendField(new Blockly.FieldDropdown([
- ["Aeroplane", "Aeroplane"],
- ["Bicycle", "Bicycle"],
- ["Boat", "Boat"],
- ["Bus", "Bus"],
- ["Car", "Car"],
- ["Chair", "Chair"],
- ["Dining", "Dining"],
- ["Table", "Table"],
- ["Motorbike", "Motorbike"],
- ["Person", "Person"],
- ["Potted", "Potted"],
- ["Plant", "Plant"],
- ["Sofa", "Sofa"],
- ["Train", "Train"],
- ["Monitor", "Monitor"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_cobjectrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "classes_objectrecognition[i.classid()]\n"
- return _code;
- }
- Blockly.Blocks['ai_model_animalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
- .appendField(new Blockly.FieldVariable("img_animalrecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_animalrecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_animalrecognition = kpu.run_yolo2(task_animalrecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_aanimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_AAOID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_aanimalrecognition = function(block) {
- var _code = "code_animalrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_ifanimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_SETROTATION)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_ifanimalrecognition = function(block) {
- // if code_animalrecognition: # if object is detected then do
- // for i_animalrecognition in code_animalrecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyanimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_FPMA)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPMA_GIOTCAD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyanimalrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_animalrecognition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_canimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_DAOIA)
- .appendField(new Blockly.FieldDropdown([
- ["Bird", "Bird"],
- ["Cat", "Cat"],
- ["Cow", "Cow"],
- ["Dog", "Dog"],
- ["Horse", "Horse"],
- ["Sheep", "Sheep"]
- ]), "DEGREE")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_canimalrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "classes_animalrecognition[i.classid()]\n"
- return _code;
- }
- Blockly.Blocks['ai_model_facerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_SISFIF)
- .appendField(new Blockly.FieldVariable("img_facerecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_facerecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_afacerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_AFID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_afacerecognition = function(block) {
- var _code = "code_facerecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_iffacerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_SETROTATION)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_iffacerecognition = function(block) {
- // if code_facerecognition: # if face is detected then do
- // for i_facerecognition in code_facerecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyfacerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPMF_GIOTFD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyfacerecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_facerecognition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_kpuload'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_LCMLM);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_FSC)
- .appendField(new Blockly.FieldTextInput("/sd/models/facedetect.kmodel"), "input")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_kpuload = function(block) {
- var input = block.getFieldValue('input');
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var _code = "kpu.load(\"" + input + "\")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_inityolo2'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIINNYFNOT)
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_F)
- .appendField(new Blockly.FieldVariable("task"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT)
- .appendField(new Blockly.FieldTextInput("0"), "input")
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT_S)
- .appendField(new Blockly.FieldTextInput("0"), "input1");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_SNOAA)
- .appendField(new Blockly.FieldTextInput("5"), "input2")
- .appendField(Blockly.Msg.CAMERINITPMF_SNOAA_S)
- .appendField(new Blockly.FieldVariable("anchor"), "varitem1");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_inityolo2 = function(block) {
- var varitem = block.getFieldValue('varitem');
- var varitem1 = block.getFieldValue('varitem1');
- var input = block.getFieldValue('input');
- var input1 = block.getFieldValue('input1');
- var input2 = block.getFieldValue('input2');
- var _code = "kpu.init_yolo2(" + varitem + ", " + input + ", " + input1 + ", " + input2 + ", " + varitem1 + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_runyolo2'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY_FNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY_SISA)
- .appendField(new Blockly.FieldVariable("camera"), "varitem1");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_runyolo2 = function(block) {
- var varitem = block.getFieldValue('varitem');
- var varitem1 = block.getFieldValue('varitem1');
- var _code = "kpu.run_yolo2(" + varitem + ", " + varitem1 + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_forward'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem")
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
- .appendField(new Blockly.FieldTextInput("3"), "input")
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
- .appendField(new Blockly.FieldVariable("task"), "varitem1")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_forward = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var variable_name1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
- var input = block.getFieldValue('input');
- var _code = "kpu.forward(" + variable_name + "," + variable_name1 + "," + input + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_deinit'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERDNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_deinit = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "kpu.deinit(" + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_if'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF)
- .appendField(new Blockly.FieldDropdown([
- ["Red", "fcr_threshold_red"],
- ["Green", "fcr_threshold_green"],
- ["Blue", "fcr_threshold_blue"]
- ]), "color")
- .appendField(Blockly.Msg.VISION_IF_R)
- .appendField(new Blockly.FieldVariable("img"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF_S)
- .appendField(new Blockly.FieldTextInput("60"), "input")
- .appendField(Blockly.Msg.HID_MOUSE_MOVE_DISTANCE);
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_if = function(block) {
- var input = block.getFieldValue('input');
- var varitem = block.getFieldValue('varitem');
- var color = block.getFieldValue('color');
- Blockly.Python.addVariable('fcr_threshold_red', 'fcr_threshold_red = (30, 100, 15, 127, 15, 127)', true);
- Blockly.Python.addVariable('fcr_threshold_green', 'fcr_threshold_green = (30, 100, -64, -8, -32, 32)', true);
- Blockly.Python.addVariable('fcr_threshold_blue', 'fcr_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
- Blockly.Python.addVariable('fcr_current_threshold', 'fcr_current_threshold = ' + color, true);
- Blockly.Python.addVariable('fcr_max_region_size_detected', 'fcr_max_region_size_detected = ' + input, true);
- var _code = "fcr_blobs = " + varitem + ".find_blobs([fcr_current_threshold], area_threshold=150)\n"
- _code += " if fcr_blobs: \n"
- _code += " for b in fcr_blobs:\n"
- _code += " if (b[2] > fcr_max_region_size_detected) or ((b[3] > fcr_max_region_size_detected)):\n"
- _code += " " + varitem + ".draw_rectangle(b[0:4])\n"
- _code += " " + varitem + ".draw_cross(b[5], b[6])\n"
- _code += " " + varitem + ".draw_rectangle(b[0], b[1]-20, b[2], 20, color=(255,255,255), fill=True)\n"
- _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"
- return _code;
- }
- Blockly.Blocks['ai_vision_gd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF_C)
- .appendField(new Blockly.FieldDropdown([
- ["X Coordinate", "0"],
- ["Y Coordinate", "1"],
- ["Width", "2"],
- ["Height", "3"],
- ]), "dropdown");
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_gd = function(block) {
- var d = block.getFieldValue('dropdown');
- var _code = "b[" + d + "]\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_dif'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CONTROLS_IF_MSG_IF)
- .appendField(new Blockly.FieldDropdown([
- ["Red", "fcr_threshold_red"],
- ["Green", "fcr_threshold_green"],
- ["Blue", "fcr_threshold_blue"]
- ]), "color")
- .appendField(Blockly.Msg.VISION_CIF)
- .appendField(new Blockly.FieldVariable("img"), "varitem");
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_dif = function(block) {
- var color = block.getFieldValue('color');
- var varitem = block.getFieldValue('varitem');
- Blockly.Python.addVariable('fc_threshold_red', 'fc_threshold_red = (30, 100, 15, 127, 15, 127)', true);
- Blockly.Python.addVariable('fc_threshold_green', 'fc_threshold_green = (30, 100, -64, -8, -32, 32)', true);
- Blockly.Python.addVariable('fc_threshold_blue', 'fc_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
- Blockly.Python.addVariable('fc_color_threshold', 'fc_color_threshold = ' + color, true);
- Blockly.Python.addVariable('fc_bounding_box_color', 'fc_bounding_box_color = (200,200,200)', true);
- var _code = "def fc_expand_roi(roi):\n"
- _code += " extra = 5\n"
- _code += " win_size = (224, 224)\n"
- _code += " (x, y, width, height) = roi\n"
- _code += " fc_new_roi = [x-extra, y-extra, width+2*extra, height+2*extra]\n"
- _code += "\n"
- _code += " if fc_new_roi[0] < 0:\n"
- _code += " fc_new_roi[0] = 0\n"
- _code += " if fc_new_roi[1] < 0:\n"
- _code += " fc_new_roi[1] = 0\n"
- _code += " if fc_new_roi[2] > win_size[0]:\n"
- _code += " fc_new_roi[2] = win_size[0]\n"
- _code += " if fc_new_roi[3] > win_size[1]:\n"
- _code += " fc_new_roi[3] = win_size[1]\n"
- _code += "\n"
- _code += " return tuple(fc_new_roi)\n"
- Blockly.Python.addFunction("fc_expand_roi", _code);
- _code = "fc_blobs = " + varitem + ".find_blobs([fc_color_threshold], area_threshold=150)\n"
- " if fc_blobs:\n"
- " for fc_blob in fc_blobs:\n"
- " fc_is_circle = False\n"
- " fc_max_circle = None\n"
- " fc_max_radius = -1\n"
- " fc_new_roi = fc_expand_roi(fc_blob.rect())\n"
- "\n"
- " for fc_c in " + varitem + ".find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=fc_new_roi):\n"
- " fc_is_circle = True\n"
- " # " + varitem + ".draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255))\n"
- " if fc_c.r() > fc_max_radius:\n"
- " fc_max_radius = fc_c.r()\n"
- " fc_max_circle = fc_c\n"
- " if fc_is_circle:\n"
- " " + varitem + ".draw_rectangle(fc_new_roi, thickness=1)\n"
- " " + varitem + ".draw_rectangle(fc_new_roi[0], fc_new_roi[1]-20, fc_new_roi[2], 20, color=(255,255,255), fill=True)\n"
- " " + 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"
- " " + varitem + ".draw_cross(fc_blob[5], fc_blob[6])\n"
- " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r(), color = fc_bounding_box_color)\n"
- " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r() + 1, color = fc_bounding_box_color)\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_dgd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_CIF_D)
- .appendField(new Blockly.FieldDropdown([
- ["X Coordinate", "0"],
- ["Y Coordinate", "1"],
- ["Width", "2"],
- ["Height", "3"],
- ]), "dropdown");
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_dgd = function(block) {
- var n = block.getFieldValue('dropdown');
- var _code = "fc_new_roi[" + n + "]\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_sif'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF);
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_D)
- .appendField(new Blockly.FieldDropdown([
- ["Black", "DETECT_SINGLE_BLACK_LINE"],
- ["White", "DETECT_SINGLE_WHITE_LINE"]
- ]), "color")
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_sif = function(block) {
- var color = block.getFieldValue('color');
- Blockly.Python.addVariable('ROI', "ROI = (0, 92, 224, 40)", true);
- Blockly.Python.addVariable('DETECT_SINGLE_BLACK_LINE', "[(0, 40)]", true);
- Blockly.Python.addVariable('DETECT_SINGLE_WHITE_LINE', "[(128, 255)]", true);
- Blockly.Python.addVariable('GRAYSCALE_THRESHOLD', "ROI = " + color, true);
- var _code = "def get_blob_value(blob):\n";
- _code += " return blob.w()\n";
- Blockly.Python.addFunction("get_blob_value", _code);
- _code = "def compare_blob(blob1, blob2):\n"
- _code += " comp_result = get_blob_value(blob1) - get_blob_value(blob2)\n"
- _code += " if comp_result > 3:\n"
- _code += " return 1\n"
- _code += " elif comp_result < -3:\n"
- _code += " return -1\n"
- _code += " else:\n"
- _code += " return 0\n"
- Blockly.Python.addFunction("compare_blob", _code);
- _code = "def get_direction(left_blob, right_blob):\n"
- _code += " MAX_WIDTH = 224\n"
- _code += " theta = 0.01\n"
- _code += " b = 3\n"
- _code += " x1 = left_blob.x() - int(0.5 * left_blob.w())\n"
- _code += " x2 = right_blob.x() + int(0.5 * right_blob.w())\n"
- _code += " w_left = x1\n"
- _code += " w_center = math.fabs(x2 - x1)\n"
- _code += " w_right = math.fabs(MAX_WIDTH - x2)\n"
- _code += " direct_ratio = (w_left + b + theta * w_center) / (w_left + w_right + 2 * b + 2 * theta * w_center) - 0.5\n"
- _code += " return direct_ratio\n"
- Blockly.Python.addFunction("get_direction", _code);
- _code = "def get_top2_blobs(blobs):\n"
- _code += " for blob in blobs:\n"
- _code += " pass\n"
- _code += " #print(blob)\n"
- _code += " # img.draw_rectangle(blob.rect())\n"
- _code += " if len(blobs) < 2:\n"
- _code += " return (None, None)\n"
- _code += " top_blob1 = blobs[0]\n"
- _code += " top_blob2 = blobs[1]\n"
- _code += " if compare_blob(top_blob1, top_blob2) == -1:\n"
- _code += " top_blob1, top_blob2 = top_blob2, top_blob1\n"
- _code += " for i in range(2, len(blobs)):\n"
- _code += " if compare_blob(blobs[i], top_blob1) == 1:\n"
- _code += " top_blob2 = top_blob1\n"
- _code += " top_blob1 = blobs[i]\n"
- _code += " elif compare_blob(blobs[i], top_blob2) == 1:\n"
- _code += " top_blob2 = blobs[i]\n"
- _code += " if top_blob1.cx() > top_blob2.cx():\n"
- _code += " return (top_blob2, top_blob1)\n"
- _code += " else:\n"
- _code += " return (top_blob1, top_blob2)\n"
- Blockly.Python.addFunction("get_top2_blobs", _code);
- _code = "def draw_direct(img, direct_ratio):\n"
- _code += " img.draw_circle(112, 112, 5)\n"
- _code += " img.draw_line((112, 112, int(112 + direct_ratio * 20), 112))\n"
- Blockly.Python.addFunction("draw_direct", _code);
- // # b 積木主體代碼
- _code = "img = sensor.snapshot()";
- _code += "blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi = ROI, merge = True)";
- _code += "if blobs:\n"
- _code += " left_blob, right_blob = get_top2_blobs(blobs)\n"
- _code += " if(left_blob == None or right_blob == None):\n"
- _code += " print(\"Out Of Range\")\n"
- _code += " lcd.display(img, oft=(8,8))\n"
- _code += " continue\n"
- _code += " else:\n"
- _code += " print(\"left blob\")\n"
- _code += " print(left_blob)\n"
- _code += " print(\"right blob\")\n"
- _code += " print(right_blob)\n"
- _code += " img.draw_circle(left_blob.cx(), left_blob.cy(), 3, thickness=2)\n"
- _code += " img.draw_circle(right_blob.cx(), right_blob.cy(), 3, thickness=2)\n"
- _code += " img.draw_line(left_blob.cx(), left_blob.cy(), right_blob.cx(), right_blob.cy(), thickness=2)\n"
- _code += " direct_ratio = get_direction(left_blob, right_blob)\n"
- _code += " img.draw_string(10, 33, \"%.2f\"%direct_ratio, mono_space=False)\n"
- _code += " rect_color = (0,0,255)\n"
- _code += " arrow_color = (255,255,255)\n"
- _code += " if float(direct_ratio) < -0.1:\n"
- _code += " img.draw_string(10, 10, \"Turn Left\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(207, 30,180, 30, color=arrow_color, thickness=3)\n"
- _code += " elif float(direct_ratio) > 0.1:\n"
- _code += " img.draw_string(10, 10, \"Turn Right\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(180, 30, 207, 30, color=arrow_color, thickness=3)\n"
- _code += " elif int(direct_ratio) == 0:\n"
- _code += " img.draw_string(10, 10, \"Go Straight\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(194, 40, 194, 20, color=arrow_color, thickness=3)\n"
- _code += " # 將條件積木中放置的代碼放在此處...\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_sgd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_G);
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_GF);
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_sgd = function(block) {
- var _code = "direct_ratio\n"
- return _code;
- }
- */
- /*
- _ ___ ____ _
- / \ |_ _| / ___| _ _ ___| |_ ___ _ __ ___
- / _ \ | | \___ \| | | / __| __/ _ \ '_ ` _ \
- / ___ \ | | ___) | |_| \__ \ || __/ | | | | |
- /_/ \_\___| |____/ \__, |___/\__\___|_| |_| |_|
- |___/
- */
- Blockly.Blocks['ai_system_restart'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_system_restart);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.ai_system_restart_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_restart = function (block) {
- Blockly.Python.definitions_.import_machine = "import machine";
- var _code = "machine.reset()\n"
- return _code;
- }
- // Blockly.Blocks['ai_system_gc'] = {
- // init: function() {
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.ai_system_enable_gc);
- // this.setInputsInline(false);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- // this.setColour("#d42b03");
- // this.setTooltip("");
- // this.setHelpUrl("");
- // }
- // };
- // Blockly.Python.ai_system_gc = function(block) {
- // Blockly.Python.definitions_.import_gc = "import gc";
- // Blockly.Python.addSetup("ai_system_gc", "gc.enable()");
- // var _code = "gc.collect()\n"
- // return _code;
- // }
- Blockly.Blocks['ai_system_run'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_system_run_python_code);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_system_run_python_code_file_path);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.ai_system_run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_run = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _code = "exec(open(" + _path + ").read())\n"
- return _code;
- }
- Blockly.Blocks['ai_system_create'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_CREATE);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_create = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _code = "def _CREATE_TEXT_FILE(_path):\n"
- _code += " f = open(_path, 'w')\n"
- _code += " f.close()\n"
- Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
- _code = "_CREATE_TEXT_FILE(" + _path + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_system_write'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendValueInput("WPATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_W);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_write = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
- var _separator = block.getFieldValue('separator');
- var _code = "def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):\n"
- _code += " f = open(_path, 'a')\n"
- _code += " f.write(_data + _sep)\n"
- _code += " f.close()\n"
- Blockly.Python.addFunction("_CREATE_TEXT_FILE_WITH_CONTENT", _code);
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
- if (_separator == "newline") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
- } else if (_separator == "comma") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ',')\n";
- } else if (_separator == "space") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ' ')\n";
- } else if (_separator == "semicolon") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ';')\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_system_input'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_INPUT);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_input = function (block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _separator = block.getFieldValue('separator');
- var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n"
- _code += " f = open(_path, 'r')\n"
- _code += " result = f.read().split(_sep)\n"
- _code += " f.close()\n"
- _code += " return result\n"
- Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
- _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
- if (_separator == "newline") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
- } else if (_separator == "comma") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ',')";
- } else if (_separator == "space") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ' ')";
- } else if (_separator == "semicolon") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ';')";
- }
- return _code;
- }
- /*
- __ ___ _____ _
- \ \ / (_) ___(_)
- \ \ /\ / /| | |_ | |
- \ V V / | | _| | |
- \_/\_/ |_|_| |_|
-
- */
- var K210_WIFI_COLOR = "#3062c1";
- Blockly.Blocks['k210_wifi_enable_disable'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_set_wifi)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_enabled, "1"],
- [Blockly.Msg.ai_wifi_disabled, "0"]
- ]), "de");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('de');
- var TOOLTIPS = {
- '1': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_enabled),
- '0': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_disabled)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_enable_disable'] = function (block) {
- var dropdown_de = block.getFieldValue('de');
- // Blockly.Python.addVariable('k210_wifi_enable_disable', '', true);
- Blockly.Python.definitions_['k210_wifi_enable_disable_def'] = '' +
- 'from machine import UART\n' +
- 'import network, time\n' +
- 'from machine import UART\n' +
- 'from Maix import GPIO\n' +
- 'from fpioa_manager import fm, board_info\n' +
- '\n' +
- '# En SEP8285\n' +
- 'fm.register(8, fm.fpioa.GPIOHS0, force=True)\n' +
- '_k210_wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)\n' +
- '\n' +
- 'fm.register(0, fm.fpioa.GPIOHS1, force=True)\n' +
- '_k210wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)\n' +
- '_k210wifi_io0_en.value(0)\n' +
- '\n' +
- 'fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)\n' +
- 'fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)\n' +
- '\n' +
- '_k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- '\n' +
- 'def _k210_wifi_enable(en):\n' +
- ' global _k210_wifi_en\n' +
- ' _k210_wifi_en.value(en)\n' +
- '';
- // TODO: Assemble Python into code variable.
- if (dropdown_de == "1") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- 'time.sleep(2)\n' +
- '_k210_wifi = network.ESP8285(_k210_uart)\n' +
- '\n';
- } else if (dropdown_de == "0") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- '\n';
- }
- return code;
- };
- Blockly.Blocks['k210_wifi_scan'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_scan.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_start_wifi_scan);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_scan_def'] = '' +
- 'def wifi_deal_ap_info(info):\n' +
- ' res = []\n' +
- ' for ap_str in info:\n' +
- ' ap_str = ap_str.split(",")\n' +
- ' info_one = []\n' +
- ' for node in ap_str:\n' +
- ' if node.startswith(\'\"\'):\n' +
- ' info_one.append(node[1:-1])\n' +
- ' else:\n' +
- ' info_one.append(int(node))\n' +
- ' res.append(info_one)\n' +
- ' return res\n' +
- '';
- var code = '' +
- '_k210_ap_scan_info = _k210_wifi.scan()\n' +
- '_k210_ap_scan_info = wifi_deal_ap_info(_k210_ap_scan_info)\n' +
- '_k210_ap_scan_info.sort(key=lambda x:x[2], reverse=True) # sort by rssi\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_scan_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_wifi_scan_result);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan_get'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_ap_scan_info';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_connect_hotspot'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_connect_wifi);
- this.appendValueInput("ssid")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_ssid);
- this.appendValueInput("pass")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_pass);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_connect_hotspot_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_connect_hotspot'] = function (block) {
- var value_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
- var value_pass = Blockly.Python.valueToCode(block, 'pass', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_connect_hotspot_def'] = '' +
- 'def _k210_wifi_reset():\n' +
- ' global _k210_uart\n' +
- ' _k210_wifi_enable(0)\n' +
- ' time.sleep_ms(200)\n' +
- ' _k210_wifi_enable(1)\n' +
- ' time.sleep(2)\n' +
- ' _k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- ' tmp = _k210_uart.read()\n' +
- ' _k210_uart.write("AT+UART_CUR=921600,8,1,0,0\\r\\n")\n' +
- ' print(_k210_uart.read())\n' +
- ' _k210_uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240)\n' +
- ' _k210_uart.write("AT\\r\\n")\n' +
- ' tmp = _k210_uart.read()\n' +
- ' print(tmp)\n' +
- ' if not tmp.endswith("OK\\r\\n"):\n' +
- ' print("reset fail")\n' +
- ' return None\n' +
- ' try:\n' +
- ' _k210_wifi = network.ESP8285(_k210_uart)\n' +
- ' except Exception:\n' +
- ' return None\n' +
- ' return _k210_wifi\n' +
- '';
- var code = '' +
- '_k210_wifi = _k210_wifi_reset()\n' +
- 'if not _k210_wifi:\n' +
- ' raise Exception("WiFi init fail")\n' +
- '\n' +
- '_k210_wifi.connect(' + value_ssid + ',' + value_pass + ')\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_get_connection_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_connection_status);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_connection_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_connection_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.isconnected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_get_config'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_wifi_info)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_get_wifi_info_name, "6"],
- [Blockly.Msg.ai_wifi_get_wifi_info_ip, "0"],
- [Blockly.Msg.ai_wifi_get_wifi_info_dns, "2"],
- [Blockly.Msg.ai_wifi_get_wifi_info_mac, "5"]
- ]), "type");
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_config_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_config'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.ifconfig()[' + dropdown_type + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_cococloud'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
- .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['k210_wifi_cococloud_create_with_item']));
- this.setColour(K210_WIFI_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip("");
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('k210_wifi_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('k210_wifi_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['k210_wifi_cococloud_create_with_container'] = {
- init: function () {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['k210_wifi_cococloud_create_with_item'] = {
- init: function () {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['k210_wifi_cococloud'] = function (block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var cococloud_data = "'{";
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var cococloud_data_final = cococloud_data + "}'";
- var code = '' +
- '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
- '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
- 'try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n'
- '';
- // cococloud_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['k210_wifi_cococloud_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_api)
- .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "api");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_cococloud_read'] = function (block) {
- var api = block.getFieldValue('api');
- Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_cococloud_read_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_property);
- this.appendValueInput("property")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_data);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_cococloud_read_data'] = function (block) {
- var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
|