ai.js 339 KB

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